首页 文章

Identity Server 4和Sustainsys SAML2拒绝非CORS请求作为CORS请求

提问于
浏览
1
  • 我正在使用带有AspNetIdentity的Identity Server 4 .

  • 我已使用Sustainsys添加了SAML身份验证提供程序 .

  • 我可以使用本地帐户成功登录 .

  • 我想将SAML身份验证提供程序链接到我的本地帐户 . 我按照流程链接外部和社交登录(因为我已经成功完成了谷歌,微软和Facebook) - 链接是https://identity.domain.com/Manage/ExternalLogins

  • 我现在可以选择链接SAML2服务

  • 我可以成功链接到外部SSO并进行身份验证;然后我被重定向回https://identity.domain.com/Manage/LinkLoginCallback但是代码在ManageController中失败,如下所示:

GetExternalLoginInfoAsync返回Null . user.Id是正确的并且存在于数据库中 .

ExternalLoginInfo info = await signInManager.GetExternalLoginInfoAsync(user.Id);
        if (info == null)
        {
            throw new ApplicationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'.");
        }

从我可以看到,外部SAML提供程序应该返回的属性集合:

public async Task<IActionResult> LinkLogin(string provider)

我怀疑这个问题可能与CORS错误有关但是我没有成功地解决这个错误,即使我已经在startup.cs中根据http://docs.identityserver.io/en/release/topics/cors.html的文档添加了以下行 .

var cors = new DefaultCorsPolicyService(_loggerFactory.CreateLogger<DefaultCorsPolicyService>())
        {
            AllowedOrigins = { "https://sso.acme.com" }
        };
        services.AddSingleton<ICorsPolicyService>(cors);

这是调试信息的日志:

  • LinkLogin redirectUrl:/ Manage / LinkLoginCallback

  • AuthenticationScheme:"Identity.Application"已成功通过身份验证 .

  • 提取SAML断言_ea533182b0cb1781868a660af48ced3529d568

  • AuthenticationScheme:"Identity.Application"已成功通过身份验证 .

  • LinkLoginCallback User.Id:a0162430-eb22-4154-bf59-0ba49bfd473d

  • 已成功处理SAML响应_402e6060e55e8e5d3b05f52ff5e5d2d7d4d786并通过身份验证test@acme.com

  • System.ApplicationException: Unexpected error occurred loading external login info for user with ID 'a0162430-eb22-4154-bf59-0ba49bfd473d'.

  • CORS request made for path: "/Saml2/Acs" from origin: "https://sso.acme.com" but was ignored because path was not for an allowed IdentityServer CORS endpoint

  • LinkLoginCallback开始

  • SAML2响应的验证条件_402e6060e55e8e5d3b05f52ff5e5d2d7d4d786

  • Http POST绑定提取的消息

  • Saml Response传递签名验证_402e6060e55e8e5d3b05f52ff5e5d2d7d4d786

  • AuthenticationScheme:"Identity.Application"已成功通过身份验证 .

更新: - 客户发回Saml请求说它不是/ Saml2 / ACS的CORS请求,因此不明白为什么我们会从Identity Server / Sustainsy获得错误

1 回答

  • 1

    这个问题的解决方案最终很简单 .

    • 我试图处理一个Saml响应(在 ExternalLoginCallback 内)使用这种方法来处理来自谷歌的微软Facebook的回复

    ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(user.Id);

    • 很明显,Saml请求的ExternalLoginCallback过程略有不同 - 您需要以下代码:

    AuthenticateResult samlResult = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);

    • 然后,您可以使用 samlResult 中的属性构建 ExternalLoginInfo 对象

    • 以下示例中提供了以下所有代码:

    https://github.com/Sustainsys/Saml2/blob/netstandard/Samples/SampleIdentityServer4/Quickstart/Account/AccountController.cs

    • 有趣的是,日志文件仍然打印出来 a CORS request made for path: "/Saml2/Acs" from origin: "https://sso.acme.com" but was ignored because path was not for an allowed IdentityServer CORS endpoint 但是这似乎对登录过程没有影响 . 所以"red-herring" .

相关问题