Swagger JWT 定义失败

发布于 2025-01-14 05:58:19 字数 791 浏览 0 评论 0原文

当我为 .Net Core 6 配置 swagger 时,我遇到了问题。

我收到下一条错误消息:

“SwaggerUIOptions”不包含“AddSecurityDefinition”的定义,并且扩展方法“SwaggerGenOptionsExtensions.AddSecurityDefinition(SwaggerGenOptions, string, OpenApiSecurityScheme)”的最佳重载需要“SwaggerGenOptions”类型的接收器

    app.UseSwaggerUI(c =>
    {
        
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My service");
        c.RoutePrefix = string.Empty;

        c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Name = "Authorization",
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer",
            BearerFormat = "JWT",
            In = ParameterLocation.Header
        });

    });

我将第二个参数添加为 null 并失败反正。

怎么了?

I am having problems in swagger when I configure it for .Net Core 6.

I am getting next error message:

'SwaggerUIOptions' does not contain a definition for 'AddSecurityDefinition' and the best overload of the extension method 'SwaggerGenOptionsExtensions.AddSecurityDefinition(SwaggerGenOptions, string, OpenApiSecurityScheme)' requires a receiver of type 'SwaggerGenOptions'

    app.UseSwaggerUI(c =>
    {
        
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My service");
        c.RoutePrefix = string.Empty;

        c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
        {
            Name = "Authorization",
            Type = SecuritySchemeType.ApiKey,
            Scheme = "Bearer",
            BearerFormat = "JWT",
            In = ParameterLocation.Header
        });

    });

I am adding the second parameter as null and fails anyway.

What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉徒眸意 2025-01-21 05:58:19

AddSecurityDefinition 应该在 AddSwaggerGen 中完成

services.AddSwaggerGen(c =>
{
     c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Name = "Authorization",
        Type = SecuritySchemeType.ApiKey,
        Scheme = "Bearer",
        BearerFormat = "JWT",
        In = ParameterLocation.Header
    });
});

AddSecurityDefinition is should be done in AddSwaggerGen

services.AddSwaggerGen(c =>
{
     c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Name = "Authorization",
        Type = SecuritySchemeType.ApiKey,
        Scheme = "Bearer",
        BearerFormat = "JWT",
        In = ParameterLocation.Header
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文