-
-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swagger authorization not working (401 error) #14
Comments
Thanks for the feedback! Bearer tokens should use Which version of services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "MyApi", Version = "v1" });
var securityScheme = new OpenApiSecurityScheme
{
Name = "JWT Authentication",
Description = "Enter JWT token.",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
Reference = new OpenApiReference
{
Id = JwtBearerDefaults.AuthenticationScheme,
Type = ReferenceType.SecurityScheme
}
};
options.AddSecurityDefinition(securityScheme.Reference.Id, securityScheme);
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ securityScheme, new string[] { } }
});
}); |
This is also nice since you don't need to have the "bearer JWTtoken" anymore in Swagger but just "JWTtoken". Just don't forget to add:
to the end of AddSwaggerGen() :) There is a nice article on this at https://codeburst.io/api-security-in-swagger-f2afff82fb8e |
@proudmonkey @improwise |
First, THANK YOU for creating and sharing the template.
Background: The ApiBoilerPlate was used to generate a WebAPI project. After adding [Authorize] attribute to the PersonsController, the endpoints require to Bearer JWT access token as expected. However, accessing any endpoint in the PersonsController will get 401.
Issue - Para authorization and bearer token are not included in the header upon submit in Swagger. External testing using Postman worked fine.
Fix - Made the following changes in Infrastructure\Installers\RegisterSwagger.cs
See below for the code -
https://github.com/workcontrolgit/EmployeeProfileWebAPIDemo/blob/master/EmployeeProfile/EmployeeProfile/Infrastructure/Installers/RegisterSwagger.cs
I will fork and submit a pull request.
The text was updated successfully, but these errors were encountered: