ASP.NETCore集成Swagger添加Authorize按钮Bearer授权
ASP.NETCore集成Swagger添加Authorize按钮Bearer授权
Swagger 添加Bearer认证Authorize按钮
C# 全选
builder.Services.AddSwaggerGen(options =>
{
#region 添加bearer token输入按钮
//添加Api接口安全需求
var securityRequirement = new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "bearer"
}
},
new string[] { }
}
};
//var securityRequirement = new Dictionary<string, IEnumerable<String>> { { "Bearer", new string[] { } }, };
options.AddSecurityRequirement(securityRequirement);
//添加Authorize授权按钮
var securityScheme = new OpenApiSecurityScheme
{
Name = "bearer",
Description = "Enter JWT: \"Authorization: Bearer {token}\"",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "bearer", // 必须小写!!!! must be lowercase
BearerFormat = "jwt",
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "bearer"
}
};
options.AddSecurityDefinition("bearer", securityScheme);
#endregion
});
C# 全选
app.UseSwagger();
app.UseSwaggerUI();
运行程序:
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网