Swashbuckle.AspNetCore: Authorization Header not being sent from Swagger UI 5.0.0-rc5
Tried both the following solutions:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1022
options.AddSecurityDefinition("basicAuth", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "Basic", // tried lowercase "basic" too.
Description = "Input your username and password to access this API",
In = ParameterLocation.Header,
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference {
Type = ReferenceType.SecurityScheme,
Id = "basicAuth" }
}, new List<string>() }
});
https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1171
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT Authorization header using the Bearer scheme.",
Name = "Authorization",
In = ParameterLocation.Header,
Scheme = "bearer",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }
},
new List<string>()
}
});
I need basic auth, and I am willing to settle for making the user put the encoded final header in. Ideally, they would be prompted for a username and password and that could automatically be encoded.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 18 (4 by maintainers)
Don’t use parameters to accomplish this as it is no longer supported by Swagger UI. To get the Authorization header included in the curl request you must define it entirely using security schemes. For reference see this comment
Want to share my configuration that works on 5.0.0-rc5:
In Startup.cs, add a global Security Definition and operation filter:
In the AuthenticationRequirementsOperationFilter add a Security Requirement to the operation by referencing the Security Definition that was added globally:
Note:
The generated UI won’t have Authorization fields in each endpoint. There should be open locks on the endpoints that had a security requirement added to them in the OperationFilter and an Authorize button should show up on the top right. Add the token to the header using the Authorize button and the endpoints will show with closed locks. Note: “Bearer” will be added automatically, so only provide the token when authorizing.
Requests should now include the Authorization header with the provided token. You can verify in the curl:
Hope it helps!
Really need a working example for bearer token.
The options below working with Bearer scheme for me:
For those who are still having trouble with this, here is the code that worked for me after few hours of trial and error.
The following example works for me (including automatic encoding of credentials). It’s worth noting that this type of question is related to understanding the Swagger specification, and how to express certain API behaviors with it, as opposed to Swashbuckle itself. When this is the case, I would encourage people to look at the Swagger documents (e.g. authentication examples) instead as they contain many examples which can be easily ported over to Swashbuckle configuration.
Anyway, here’s a working example for basic Auth (derived from the Swagger docs):
I’m also experiencing the same issue where the UI is not adding the authorization header.
I’m using token authentication that is applied conditionally based on attrbiutes of my controller, but with very similar code in an IOperationFilter:
The UI is generated correctly but the header is not added to the request.
using 5.0.0-rc5
I met the same issue before and resolved it.
Now the available Authorization header works fine.
Please check my latest sample using SwashBuckle v5.5.1 and netcore 3.1 https://github.com/capcom923/MySwashBuckleSwaggerWithJwtToken
@pnavk Thank’s you led me to the solution which was that I had
options.OperationFilter<SecurityRequirementsOperationFilter>()
in my startup.cs! This was overriding anything I did thanks to this https://github.com/mattfrear/Swashbuckle.AspNetCore.Filters/blob/master/src/Swashbuckle.AspNetCore.Filters/SecurityRequirementsOperationFilter/SecurityRequirementsOperationFilter.cs#L20Similar one here: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/master/test/WebSites/OAuth2Integration/ResourceServer/Swagger/SecurityRequirementsOperationFilter.cs#L27
All I had to do was add the following to my
AddSwaggerGen
and the problem was solved.The key here being
"oauth2"
since that references the hardcodedOpenApiScheme.OpenApiReference
.This is working for me under 5.0.0-rc5:
In Startup.cs in ConfigureServices:
NOTE: I filter on
[Authorize]
attribute[AllowAnonymous]
method attributes.in
AddAuthHeaderOperationFilter
- code removed for clarity.@domaindrivendev That did not work.
Click authorize
Fill out info and click the authorize button
Expand an operation
Click “Try it out”
Click “Execute”
401! And there is no “Authorize” header in the request payload.
But Swashbuckle needs to understand the Swagger body to make use of it in the UI does it not? How else does the UI know to create Auth headers, and encode parameters?