Swagger(C#)不添加授权标题

发布于 2025-02-06 18:12:56 字数 2098 浏览 3 评论 0原文

我试图让Swagger与客户凭证流程一起工作。

builder.Services.AddSwaggerGen(c =>
{
    c.SchemaFilter<SchemaFilter>();

    c.SwaggerDoc("v1", new OpenApiInfo { Title = "The API", Version = "v1" });

    var basePath = System.AppContext.BaseDirectory;
    string docFileName = Directory.GetFiles(basePath, "*.xml").First();
    var docFile = Path.Combine(basePath, docFileName);

    if (File.Exists(docFile))
    {
        c.IncludeXmlComments(docFile);
    }

    // Makes the UI appear and be able to get a token.
    c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
    {
        Description = "Bearer token",
        In = ParameterLocation.Header,
        Name = "Authorization",
        Scheme = "Bearer",
        BearerFormat = "JWT",
        Type = SecuritySchemeType.OAuth2,
        Flows = new OpenApiOAuthFlows()
        {
            ClientCredentials = new OpenApiOAuthFlow()
            {
                AuthorizationUrl = new Uri("http://www.localhost:5000/connect/authorize"),
                TokenUrl = new Uri("http://www.localhost:5000/connect/token"),
                Scopes = new Dictionary<string, string>() { { "thescope", "The scope" } }
            }
        }
    });

    // Docs claim this is necessary; doesn't say what it does in addition to the above.
    c.AddSecurityRequirement(new OpenApiSecurityRequirement()
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme
                },
                Scheme = "Bearer", // https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
                In = ParameterLocation.Header
            },

            new List<string>()
        }
    });

在Swagger网页上出现 授权按钮,我可以使用client_idclient_secret获得令牌。

但是,Swagger不使用令牌。没有授权将标题添加到任何请求中。为什么?

I'm trying to get Swagger working with a client credentials flow.

builder.Services.AddSwaggerGen(c =>
{
    c.SchemaFilter<SchemaFilter>();

    c.SwaggerDoc("v1", new OpenApiInfo { Title = "The API", Version = "v1" });

    var basePath = System.AppContext.BaseDirectory;
    string docFileName = Directory.GetFiles(basePath, "*.xml").First();
    var docFile = Path.Combine(basePath, docFileName);

    if (File.Exists(docFile))
    {
        c.IncludeXmlComments(docFile);
    }

    // Makes the UI appear and be able to get a token.
    c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme()
    {
        Description = "Bearer token",
        In = ParameterLocation.Header,
        Name = "Authorization",
        Scheme = "Bearer",
        BearerFormat = "JWT",
        Type = SecuritySchemeType.OAuth2,
        Flows = new OpenApiOAuthFlows()
        {
            ClientCredentials = new OpenApiOAuthFlow()
            {
                AuthorizationUrl = new Uri("http://www.localhost:5000/connect/authorize"),
                TokenUrl = new Uri("http://www.localhost:5000/connect/token"),
                Scopes = new Dictionary<string, string>() { { "thescope", "The scope" } }
            }
        }
    });

    // Docs claim this is necessary; doesn't say what it does in addition to the above.
    c.AddSecurityRequirement(new OpenApiSecurityRequirement()
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme
                },
                Scheme = "Bearer", // https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
                In = ParameterLocation.Header
            },

            new List<string>()
        }
    });

On the Swagger web page an Authorize button appears and I can use the client_id and client_secret to get a token.

However, Swagger does not use the token. No Authorize header is added to any requests. Why?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文