ASP.NET Core的swagger UI中缺少authToken字段怎么办

2023-04-24 12:47:00 core 字段 缺少

如果ASP.NET Core的swagger UI中缺少authToken字段,可以使用以下步骤来添加它:

1.首先,请确保安装了Swashbuckle.AspNetCore NuGet包,这是ASP.NET Core中swagger UI的必备包。

2.接下来,在Startup.cs文件中,添加以下代码:

services.AddSwaggerGen(c =>
{
    c.AddSecurityDefinition("Bearer", new ApiKeyScheme
    {
        In = "header",
        Description = "Please enter JWT with Bearer into field",
        Name = "Authorization",
        Type = "apiKey"
    });
});

3.然后,在Configure方法中,添加以下代码:

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    c.AddSecurityDefinition("Bearer", new ApiKeyScheme
    {
        In = "header",
        Description = "Please enter JWT with Bearer into field",
        Name = "Authorization",
        Type = "apiKey"
    });
    c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
    {
        { "Bearer", Enumerable.Empty<string>() }
    });
});

4.最后,重新启动应用程序,就可以在swagger UI中看到authToken字段了。

以上就是在ASP.NET Core的swagger UI中添加authToken字段的方法,只要按照步骤操作,就可以实现添加authToken字段,从而更好地使用swagger UI。

相关文章