0

Previously when using .NET 9, I could authenticate in Swagger using an Authorize button and auth-protected endpoints would have a "lock" icon on them:

before

But now when using .NET 10 and latest version of Swashbuckle:

<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.0.1" />

the "Authorize" button is simply gone. And the lock icons on the endpoints as well.

After some googling I found a couple've SO's that recommended adding these AddSecurityDefinition and OpenApiSecurityRequirement to the AddSwaggerGen. But I couldn't use their answers directly since it seems that they changed some things around in the latest version and it wouldn't compile.

I finally managed to get a version that would compile and run:

services.AddSwaggerGen(options =>
{
    // ...

    options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Name = "Authorization",
        Description = "JWT Authorization header using the Bearer scheme. Enter 'Bearer' [space] and then your token in the text input below.",
        In = ParameterLocation.Header,
        Type = SecuritySchemeType.Http,
        Scheme = "Bearer",
        BearerFormat = "JWT"
    });

    options.AddSecurityRequirement(doc => new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecuritySchemeReference("Bearer"),
            new List<string>()
        }
    });
});

And the "Authorize" button is back! But the lock-icons are not:

after

And most importantly, even though the Authorize button works and I can "log in", it doesn't actually add the Authorization header to the requests...

missing-auth-header

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.