I am developing an ASP.NET Core Web API application. I am using Swagger and have applied JWT Bearer token authentication, but when I try to pass the Bearer token from Swagger UI, I am getting a 401 error.
My code:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.SwaggerDoc("v2", new OpenApiInfo { Title = "My API", Version = "v2" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
});
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Versioned API v1.0");
c.SwaggerEndpoint("/swagger/v2/swagger.json", "Versioned API v2.0");
c.DocExpansion(DocExpansion.None);
});
Generated token:
Adding token in Swagger:
Unauthorized response: