我认为通过修改index.html来发送授权头不是一个好方法。您只能添加一些设置来实现这一点。
这是我的解决方案: 1.
在Starup.cs ConfigureServices方法中添加设置
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(config => {
config.SwaggerDoc("v1", new OpenApiInfo() { Title = "WebAPI", Version = "v1" });
config.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
config.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
}
2.在Startup.cs 配置方法中添加设置
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API Document"));
}
添加设置后,运行这个项目,可以找到一个 Authorization button
swagger page,可以用来设置授权头。