0

I have ASP.Net Core 2.1 app (API). Need to include the Authorize button in the swagger docs

This app is using Token-Based Authorization with AWS Cognito & here is Swagger Configuration

 services.AddSwaggerGen(c =>
        {
            var security = new Dictionary<string, IEnumerable<string>>
            {
                {"Bearer", new string[] { }},
            };

            c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info()
            {
                Version = "v1.0",
                Title = "My API",
                Description = "My WebAPIs build on .Net Core 2.1",
                Contact = new Swashbuckle.AspNetCore.Swagger.Contact()
                {
                    Name = "Ram",
                    Email = "myemail@email.com"
                }
            });
            c.AddSecurityDefinition("OAut2", new ApiKeyScheme
            {
                Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
                In = "header",
                Name = "Authorization",
                Type = "apikey"

            });
            //c.AddSecurityRequirement(security);
            //c.OperationFilter<SecurityRequirementsOperationFilter>();

        });

The above configuraiton adds a Authorize button at the top of the document. But when I click the button it display as

enter image description here

But with below link reference.Attached is desired.

https://mattfrear.com/2018/07/21/add-an-authorization-header-to-your-swagger-ui-with-swashbuckle-revisited/

enter image description here

Also when I uncomment the below line in configuration,it adds a lock icon on every endpoint while I need to add the lock icon on the endpoints that need authentication.

c.AddSecurityRequirement(security);

How do I get rid of these 2 errors?

Thanks!

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Change `Type = "apikey"` to `Type = "apiKey"` (upperface K). – Helen Oct 03 '19 at 12:47
  • @Helen, Thanks! yes it fixes. can you please check the second part of this post i.e., lock icon – Kgn-web Oct 03 '19 at 12:49
  • [According to this answer](https://stackoverflow.com/a/54696059/113116) you need to configure operation-level `.Security` instead of global `.AddSecurityRequirement`. – Helen Oct 03 '19 at 14:33
  • for anyone interested check this [SO Answer](https://stackoverflow.com/a/49478590/4381062) – ash Nov 02 '21 at 17:56

0 Answers0