Table of Contents

AddBasicAuthenticationSecurity Method

Definition

Namespace
Codebelt.Extensions.Swashbuckle.AspNetCore
Assemblies
Codebelt.Extensions.Swashbuckle.AspNetCore.dll
Source
src/Codebelt.Extensions.Swashbuckle.AspNetCore/SwaggerGenOptionsExtensions.cs

AddBasicAuthenticationSecurity(SwaggerGenOptions)

Adds support for AuthN/AuthZ using the Basic security scheme.

public static SwaggerGenOptions AddBasicAuthenticationSecurity(this SwaggerGenOptions options)

Parameters

options SwaggerGenOptions

The SwaggerGenOptions to extend.

Returns

SwaggerGenOptions

A reference to options so that additional calls can be chained.

Examples

Document HTTP Basic authentication:

using System;
using Codebelt.Extensions.Swashbuckle.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace MySwaggerExample;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddControllers();
        builder.Services.AddEndpointsApiExplorer();

        builder.Services.AddSwaggerGen(options =>
        {
            options.AddBasicAuthenticationSecurity();
        });

        var app = builder.Build();
        if (string.Equals(app.Environment.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.MapControllers();
        app.Run();
    }
}