.Net5 Core 配置 Swagger 并进行全局Token添加
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddNewtonsoftJson();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
c.DocInclusionPredicate((docName, description) => true);
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = "JWT授权(数据将在请求头中进行传输) 在下方输入Bearer {token} 即可,注意两者之间有空格",
Name = "Authorization",//jwt默认的参数名称
In =ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中)
Type =SecuritySchemeType.ApiKey
});
//认证方式,此方式为全局添加
c.AddSecurityRequirement(new OpenApiSecurityRequirement {
{ new OpenApiSecurityScheme
{
Reference = new OpenApiReference()
{
Id = "Bearer",
Type = ReferenceType.SecurityScheme
}
}, Array.Empty<string>() }
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, XAdminSettingService xAdminSetting)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
#region 开发环境下启用Swagger
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1")) ;
#endregion
}
}