'How to include Authorization header using Swagger in .NET Core 6

I was used to use swagger in older versions of .NET Core. However, now using .NET Core 6, I'm not being able to add Authorization header using Authorize button in swagger.

How could I do this?

I already added this to Program.cs:

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc(API_VERSION, new OpenApiInfo
    {
        Title = PROJECT_NAME,
        Version = API_VERSION
    });

    var xmlFile = Assembly.GetExecutingAssembly().GetName().Name + XML_EXTENSION;
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

    c.IncludeXmlComments(xmlPath);

    c.AddSecurityDefinition(SECURITY_TYPE, new OpenApiSecurityScheme
    {
        Description = SECURITY_DESCRIPTION,
        In = ParameterLocation.Header,
        Name = SECURITY_HEADER_NAME,
        Type = SecuritySchemeType.ApiKey,
        Scheme = SECURITY_TYPE
    });

    c.OperationFilter<SecurityRequirementsOperationFilter>();
});

and this:

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint(SWAGGERFILE_PATH, PROJECT_NAME + API_VERSION);
    c.RoutePrefix = string.Empty;
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source