Reputation: 85
I'm migrating from Swashbuckle to Swagger-Net and need help with specifying user credentials to get a bearer token and auth with it.
Everything is working fine if I'll specify the bearer token explicitly, but I want a way to specifiy username, password and ClienId, then get a bearer token and include it in all requests.
In Swashbuckle I was able to achive it using this article. Is any way to make it working with Swagger-net other way than in article above?
Upd: I tried to use OAuth, I was able to authorize, but bearer token is not adding to each request. More over, if i press auth on method available authorizations are empty. What's wrong?
httpConfiguration
.EnableSwagger(c =>
{
c.OAuth2("oauth2")
.Flow("password")
.TokenUrl("/token");
c.OperationFilter<AssignOAuth2SecurityRequirements>();
});
.EnableSwaggerUi(c =>
{
c.EnableOAuth2Support("test-client-id", "test-realm", "Swagger UI");
});
Upvotes: 2
Views: 773
Reputation: 85
Finally, I made it working. The issue was in wrong AssignOAuth2SecurityRequirements filter: I specified Bearer instead oauth2 security
public class AssignOAuth2SecurityRequirements : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
// Correspond each "Authorize" role to an oauth2 scope
var scopes = apiDescription.ActionDescriptor.GetFilterPipeline()
.Select(filterInfo => filterInfo.Instance)
.OfType<AuthorizeAttribute>()
.SelectMany(attr => attr.Roles.Split(','))
.Distinct();
if (scopes.Any())
{
if (operation.security == null)
operation.security = new List<IDictionary<string, IEnumerable<string>>>();
var oAuthRequirements = new Dictionary<string, IEnumerable<string>>
{
{ "oauth2", scopes }
};
operation.security.Add(oAuthRequirements);
}
}
}
Upvotes: 1