Skip to content
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

Add swagger 'Authorization' feature #110

Open
Ajay100000 opened this issue Oct 7, 2022 · 2 comments
Open

Add swagger 'Authorization' feature #110

Ajay100000 opened this issue Oct 7, 2022 · 2 comments
Labels
good first issue Good for newcomers improvements Larger code contributions tests Pull requests that update a tests project up-for-grabs Good for newcomers

Comments

@Ajay100000
Copy link

Ajay100000 commented Oct 7, 2022

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.

@madeyoga madeyoga added improvements Larger code contributions new-feature New feature labels Oct 8, 2022
@madeyoga
Copy link
Owner

madeyoga commented Oct 8, 2022

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

@madeyoga madeyoga added tests Pull requests that update a tests project up-for-grabs Good for newcomers good first issue Good for newcomers and removed new-feature New feature labels Oct 8, 2022
@Ajay100000
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers improvements Larger code contributions tests Pull requests that update a tests project up-for-grabs Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants