且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

迁移到Swashbuckle.AspNetCore版本5时,在Swagger UI中进行承载身份验证

更新时间:2023-12-06 09:18:58

最终通过反复试验使此工作正常进行.这是对我有用的代码:

Got this working in the end by trial and error. This is the code that works for me:

c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
    Description =
        "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
    Name = "Authorization",
    In = ParameterLocation.Header,
    Type = SecuritySchemeType.ApiKey,
    Scheme = "Bearer"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
    {
        new OpenApiSecurityScheme
        {
            Reference = new OpenApiReference
            {
                Type = ReferenceType.SecurityScheme,
                Id = "Bearer"
            },
            Scheme = "oauth2",
            Name = "Bearer",
            In = ParameterLocation.Header,

        },
        new List<string>()
    }
});

我怀疑那里可能设置了实际上不需要显式设置的属性,但是上面的内容对我有用.

I suspect there are probably properties being set there that don't actually need to be explicitly set, but the above is working for me.