我已经为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中可见,但是当我单击该按钮时,它显示以下错误:
有人可以指出我的问题吗?
答案 0 :(得分:2)
您的行应为:
c.AddSecurityDefinition("Bearer", new ApiKeyScheme {
In = "header",
Description = "Please Enter Authentication Token",
Name = "Authorization", Type = "apiKey"
});
as Type在Swashbuckle内部转换为策略模式。 可能的值为:
可能您可以在Swashbuckle repo内提交错误,因为除非可以覆盖它,否则它可能是一个枚举,否则不会引起混乱。