AuthEndpoints icon indicating copy to clipboard operation
AuthEndpoints copied to clipboard

Add swagger 'Authorization' feature

Open Ajay100000 opened this issue 2 years ago • 2 comments

Hi,

This is looking very good, but the swagger could be improved by using its authorization feature. I have this working, and this is what I did.

In program.cs: after the following lines, add the "// To Enable authorization using Swagger (JWT)" section of code

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); options.IncludeXmlComments(xmlPath);

// To Enable authorization using Swagger (JWT)
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
{
    Name = "Authorization",
    Type = SecuritySchemeType.ApiKey,
    Scheme = "Bearer",
    BearerFormat = "JWT",
    In = ParameterLocation.Header,
    Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
});

options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
    {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"
                }
            },
            Array.Empty<string>()
    }
});

To use, get the bearer token, and press the green Authorize button.

In the input box type the word Bearer followed by a space and paste the bearer token. Then press the authorize button and the close button.

Features like /users/me, should now work as expected. This worked great for me, and I think should be part of the demo code.

Hope this helps.

Ajay100000 avatar Oct 07 '22 15:10 Ajay100000

Hi Ajay, thanks for opening this issue! Would you like to submit a PR for this?

madeyoga avatar Oct 08 '22 04:10 madeyoga

I don't have time right now, am very busy with several projects, that is why I included the solution above, so feel free to add it.

Ajay100000 avatar Oct 12 '22 13:10 Ajay100000