Swagger授权标头在ASP.NET Core项目中不可见

时间:2018-12-11 14:11:39

标签: asp.net swagger asp.net-core-2.0 swashbuckle

我已经为asp.net核心项目包含了swagger软件包( Swashbuckle.AspNetCore )。 swagger软件包是最新的 4.0.1 版本。我尝试使用以下代码在swagger ui中启用“授权”按钮:

// 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>() },
    });
});

该按钮在swagger ui中可见,但是当我单击该按钮时,它显示以下错误:

enter image description here

有人可以指出我的问题吗?

1 个答案:

答案 0 :(得分:2)

您的行应为:

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

as Type在Swashbuckle内部转换为策略模式。 可能的值为:

  • 基本
  • apiKey
  • oauth2

可能您可以在Swashbuckle repo内提交错误,因为除非可以覆盖它,否则它可能是一个枚举,否则不会引起混乱。