I want to generate swagger document that puts authorization token to header after loging in successfully with authorization button. I used Password flow with TokenUrl to link the login API to the authorization button
My configs in code
startup.cs
public void ConfigureServices(IServiceCollection services)
{
// this endpoint links to login API, require user name and password
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "1.0.0" });
var uri = new Uri("/api/Account/IdentityLoginRequestFormData", UriKind.Relative);
c.AddSecurityDefinition("basic", new OpenApiSecurityScheme
{
// ..........
Type = SecuritySchemeType.OAuth2,
Name = "Authorization",
Flows = new OpenApiOAuthFlows
{
Password = new OpenApiOAuthFlow
{
TokenUrl = uri,
}
},
In = ParameterLocation.Header,
Scheme = "basic"
});
c.OperationFilter<SecureEndpointAuthRequirementFilter>();
});
// ...................
}
SecureEndpointAuthRequirementFilter is the class that filters all controller actions with Authorized attribute on it
controller.cs
[Route("api/[controller]")]
public class TestController : BaseAPIController
{
public TestController(IRepository repository) : base(repository)
{ }
[Authorize(AuthenticationSchemes = "BearerScheme")]
[HttpGet("secure")]
public IActionResult GetSomethingPrivate()
{
return Ok("secret");
}
[HttpGet("public")]
public IActionResult GetSomethingPublic()
{
return Ok("hey");
}
}
after login using authorization button, and use postman to send secure request to /api/test/secure endpoint, it shows the error response body
can't parse JSON. Raw result:
No authentication handlers are registered. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("BearerScheme",...)?
Did I do something wrong or missing something in configuration Thanks