CORS 政策阻止了 b2c 登录请求

时间:2021-03-22 09:35:03

标签: javascript http cors fetch azure-ad-b2c

我正在开发一个 Web 应用程序,其中用户会收到来自 Web 应用程序的质询,其中包含指向另一个网站的 URL,并要求用户注册。

注册页面是使用 Azure AD B2C 中的注册流程制作的。

从我的 ASP.NET Core API 发送的响应如下所示:

var redirectUrl = Url.Action(nameof(SignupController.Return), "Signup");
        var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
        properties.Items[AzureAdB2COptions.PolicyAuthenticationProperty] = AzureAdB2COptions.SignUpPolicyId;
        return Challenge(properties, OpenIdConnectDefaults.AuthenticationScheme);

使用 Javascript 中的 Fetch API 请求挑战,如下所示:

            fetch(`/api/invitations/${invitationId}`, {
            method: 'PUT',
            mode: 'cors'
        }).then(response => {
            // HTTP 301 response
            if (response.redirected) {
                console.log(response.url);
                window.location.href(response.url);
            }
        })
            .catch(function (err) {
                console.log(err);
            });

但是我不断收到此错误:

Error message

据我所知,这是服务器问题,但我想它在 b2clogin 上?我真的不知道我应该如何更新 CORS 政策。我可以通过单击轻松访问如上所示的获取 URL,但 javascript 代码本身无法重定向到它。我确定这只是一个简单的修复。希望有人帮忙。

1 个答案:

答案 0 :(得分:0)

由于上下文有限,我不确定您在这里尝试实现的目标。但是,要修复此错误,请在本地运行 Azure CLI 命令:

az webapp cors add --resource-group myResourceGroup --name api-name --allowed-origins 'http://localhost:44315'

详细了解应用服务 here 中的 CORS 政策。