首页 > 解决方案 > CORB OpenID Connect /connect/authorize 端点(Identity Server 4)

问题描述

我目前遇到了一个奇怪的问题。我们正在托管一个使用 Identity Server 4 作为联合网关的 ASP.CORE 2.1 应用程序。

最初使用 Chrome 导航到该站点时,它是空的 - 在控制台中只留下一个小提示,表明 CORB 已阻止某些内容。

在此处输入图像描述

调查 fiddler 似乎它阻止了回发到应用程序/signin-oidc中间件的“隐藏”表单。

Fiddler 显示来自身份服务器的响应

但奇怪的是,刷新后一切正常,CORB 不再是问题。查看提琴手中相应的“好”响应,相同的标题是相同的。

我可以验证来自 Identity Server 的 form_post 没有到达提琴手中的应用程序,调试 OpenID 事件和每个请求,我得到了 nada。

app.Use(async (context, next) =>
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                   // look to see if bearer is present instead of cookies
                    string auth = context.Request.Headers["Authorization"];
                    if (string.IsNullOrEmpty(auth) || !auth.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
                    {
                       // request needs to be authenticated
                        await context.ChallengeAsync();
                    }
                    else
                    {
                        await next();
                    }
            }
            else
            {
                // all is good, continue
                await next();
            }
        });

为什么 CORB 会阻止初始请求?它可以是浏览器导航与刷新中的一种行为吗?

这是我正在调试的事件的客户端配置:

services.AddAuthentication(opts =>
            {
                opts.DefaultChallengeScheme = "oidce";
                opts.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
                .AddCookie(opts =>
                {
                    opts.SessionStore = new MemoryCacheTicketRepository();
                    opts.ExpireTimeSpan = TimeSpan.FromHours(2);
                })
                .AddIdentityServerAuthentication("Bearer", opts =>
                {
                    ...
                })
                .AddOpenIdConnect("oidce", opts =>
                {
                    ...
                    opts.Events = new Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents()
                    {
                        OnAuthenticationFailed = async p =>
                        {
                            var test = p;
                        },
                        OnAuthorizationCodeReceived = async p =>
                        {
                            var test = p;
                        },
                        OnMessageReceived = async p =>
                        {
                            var test = p;
                        },
                        OnRemoteFailure = async p =>
                        {
                            var test = p;
                        },
                        OnTicketReceived = async p =>
                        {
                            var test = p;
                        },
                        OnTokenResponseReceived = async p =>
                        {
                            var test = p;
                        },
                        OnTokenValidated = async p =>
                        {
                            var test = p;
                        }

                    };
                });

'Access-Control-Allow-Headers'由于在好的或坏的响应中出现的标题没有区别,我猜'Content-Type'这不是问题。会是什么呢?

标签: asp.net-coreidentityserver4openid-connect

解决方案


按照 Vidmantas Blazevicius 的评论,我尝试添加 CSPS 标头,它确实有效。

小问题,起初我在为 SPA 服务的 ASP CORE 应用程序中应用了标头。

app.Use(async (context, next) =>
{
    context.Response.Headers.Add("Content-Security-Policy",
                                "default-src 'self' https://localhost:5000; script-src 'unsafe-eval'; script-src-elem 'unsafe-inline';");
        if (!context.User.Identity.IsAuthenticated)
        {
                // look to see if bearer is present instead of cookies
            string auth = context.Request.Headers["Authorization"];
            if (string.IsNullOrEmpty(auth) || !auth.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
                {
                   // request needs to be authenticated
                    await context.ChallengeAsync();
                }
                else
                {
                    await next();
                }
        }
        else
        {
            // all is good, continue
            await next();
        }
    });

这似乎并没有真正起到作用,所以我尝试将它添加到应用程序正在服务的 index.html 中。

<meta http-equiv="Content-Security-Policy" content="default-src 'self' https://localhost:5000; script-src 'unsafe-inline' 'unsafe-eval' 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;">

到目前为止,它似乎确实有效。


推荐阅读