0

I have included the swagger package(Swashbuckle.AspNetCore) for my asp.net core project. The swagger package is of latest 4.0.1 version. I tried to enable the Authorize button in the swagger ui using below code:

// Register the Swagger generator
services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new Info { Title = "Sample API", Description = "Swagger Sample API", Version = "v1" });
    c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please Enter Authentication Token", Name = "Authorization", Type = "SampleApiKey" });
    c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
        { "Bearer", Enumerable.Empty<string>() },
    });
});

The button can be visible in the swagger ui but when i clicked the button, it shows below error:

enter image description here

Can anyone point me out what's the problem here?

Dale K
  • 25,246
  • 15
  • 42
  • 71
Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
  • Possible duplicate of [Setting up Swagger (ASP.NET Core) using the Authorization headers (Bearer)](https://stackoverflow.com/questions/43447688/setting-up-swagger-asp-net-core-using-the-authorization-headers-bearer) – Michael Freidgeim Apr 01 '19 at 08:27

1 Answers1

3

Your line should be:

c.AddSecurityDefinition("Bearer", new ApiKeyScheme { 
        In = "header",
        Description = "Please Enter Authentication Token",
        Name = "Authorization", Type = "apiKey"
});

as Type is being transformed inside Swashbuckle as strategy pattern. Possible values are:

  • basic
  • apiKey
  • oauth2

Possibly you can file a bug inside Swashbuckle repo, cos unless it can be overriden it could be for instance an enum, to not bring confusion.

Vladas Maier
  • 2,054
  • 2
  • 22
  • 34
WueF
  • 450
  • 8
  • 15