可以使用OIDC通过我的授权服务器授权Swagger

发布于 2025-02-07 10:14:44 字数 2547 浏览 2 评论 0原文

我正在使用swashbuckle

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v2", new OpenApiInfo { Title = "API", Version = "v2" });
    c.AddSecurityDefinition("OpenId", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.OpenIdConnect,
        Name = "Authorization",
        In = ParameterLocation.Header,
        Scheme = "Bearer",
        Flows = new OpenApiOAuthFlows
        {
            AuthorizationCode = new OpenApiOAuthFlow
            {
                AuthorizationUrl = new Uri($"{authority}connect/authorize"),
                TokenUrl = new Uri($"{authority}connect/token"),
                Scopes = new Dictionary<string, string>
                {
                    {
                        "openid", "openid"
                    },
                    {
                        "api", "api"
                    },
                },
            },
        },
        OpenIdConnectUrl = new Uri($"{authority}.well-known/openid-configuration"),
    });

    c.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "OpenId",
                },
            },
            new List<string> { "api", "openid" }
        },
    });
});

在此之后

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v2/swagger.json", "API v2");
    c.OAuthUsePkce();
    c.OAuthClientId(Configuration.GetRequiredSection("SwaggerOptions:ClientId").Value);
    c.OAuthClientSecret(Configuration.GetRequiredSection("SwaggerOptions:ClientSecret").Value);
    c.EnablePersistAuthorization();
    c.OAuthScopes("api", "openid");
});

配置的我看到结果swagger.json似乎是正确的, noreferrer”>在docs

”

但肯定是错误的 - 我得到cors header' Access-Control-Allow-Origin'缺失发现请求拒绝的原因,同时返回了正确的配置,并使用200 OK

“在此处输入图像描述”

我错过了什么?

I'm using Swashbuckle configured as

services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v2", new OpenApiInfo { Title = "API", Version = "v2" });
    c.AddSecurityDefinition("OpenId", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.OpenIdConnect,
        Name = "Authorization",
        In = ParameterLocation.Header,
        Scheme = "Bearer",
        Flows = new OpenApiOAuthFlows
        {
            AuthorizationCode = new OpenApiOAuthFlow
            {
                AuthorizationUrl = new Uri(
quot;{authority}connect/authorize"),
                TokenUrl = new Uri(
quot;{authority}connect/token"),
                Scopes = new Dictionary<string, string>
                {
                    {
                        "openid", "openid"
                    },
                    {
                        "api", "api"
                    },
                },
            },
        },
        OpenIdConnectUrl = new Uri(
quot;{authority}.well-known/openid-configuration"),
    });

    c.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type = ReferenceType.SecurityScheme,
                    Id = "OpenId",
                },
            },
            new List<string> { "api", "openid" }
        },
    });
});

And after that

app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v2/swagger.json", "API v2");
    c.OAuthUsePkce();
    c.OAuthClientId(Configuration.GetRequiredSection("SwaggerOptions:ClientId").Value);
    c.OAuthClientSecret(Configuration.GetRequiredSection("SwaggerOptions:ClientSecret").Value);
    c.EnablePersistAuthorization();
    c.OAuthScopes("api", "openid");
});

I see resulting swagger.json seems to be correct, as it declared at the docs

enter image description here

But something goes definitely wrong - I get CORS header 'Access-Control-Allow-Origin' missing reason for discovery request rejecting, simultaneously it returns a correct configuration with 200 ok

enter image description here

What have I missed?

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

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

发布评论

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

评论(1

め七分饶幸 2025-02-14 10:14:44

最终,我能够使它工作。在这种情况下,我误解了哪个部分确实需要CORS。为了解决这个问题,我添加了Swagger UI主机以允许在Auth Server端的主机,然后在那里切换CORS。现在,一切都很好!

Eventually, I was able to get this to work. I was misunderstanding which part does require CORS in this case. To fix that, I added my Swagger UI host to allowed hosts on auth server side and switch CORS on there. Now, all work fine!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文