Home > database >  Swagger - Add more scopes to Authorize button
Swagger - Add more scopes to Authorize button

Time:08-08

I have an API that uses Swagger. Authentication and authorization are done with IdentityServer 4. In the swagger UI, there's an Authorize button. If I press it I get this popup:

enter image description here

I can select my scope, press the authorize button, and I'll be redirected to a login screen, which all works.

My problem is that I need more scopes to be added to this list. Right now there is just one scope available here. How can I add another scope to this list?

CodePudding user response:

If you are using Swashbuckle, at your OpenApiOAuthFlow object you have to define the Scopes Dictionary. Should look like this:

services.AddSwaggerGen(c =>
{
    // omitted code
    
    c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.OAuth2,
        Flows = new OpenApiOAuthFlows
        {
            Implicit = new OpenApiOAuthFlow
            {
                AuthorizationUrl = new Uri("..."),
                TokenUrl = new Uri("..."),
                
                // add your scopes here
                Scopes = new Dictionary<string, string>
                {
                    { "scope1", "Optional friendly description." },
                    { "scope2", string.Empty },
                    { "scope3", string.Empty }
                }
            }
        }
    });
    
    // omitted code
});
  • Related