5

将 ITfoxtec 用于 ASP.NET Core 3.0 时遇到问题。
作为上下文,我正在尝试在 Web 应用程序和第三方登录服务之间建立连接。为了预先封装一些可能性,第三方可以访问我们的 metadata-url 并为我们的 web 应用程序配置他们的服务。

所需的用户工作流程:

  • 用户进入网络应用程序;
  • 用户单击将用户重定向到登录服务的按钮;
  • 用户登录服务并重定向回给定的 returnURL;
  • 之后,Web 应用程序根据提供的 sso-cookie 确定权限。

到目前为止采取的步骤:

  • 在 appsettings.json 中添加了 Saml2 部分,其中包含我们的 metadata.xml 和颁发者。颁发者名称等于 metadata.xml 中提供的给定 EntityID。它在给定的上下文中是匿名的,如下所示:
"Saml2": {
    "IdPMetadata": "wwwroot/SAML/Metadata.xml",
    "Issuer": "myIssuerName",
    "SignatureAlgorithm": "http://www.w3.org/2000/09/xmldsig#rsa-sha1",
    "CertificateValidationMode": "ChainTrust",
    "RevocationMode": "NoCheck",
    "SigningCertificateFile": "\\licenses\\certificate.pfx",
    "SigningCertificatePassword": "password1"
}, 
  • 在 startup.cs 中添加了 Saml2Configuration;
    services
        .Configure<Saml2Configuration>(Configuration.GetSection("Saml2"))
        .Configure<Saml2Configuration>(configuration =>
        {
            configuration.SigningCertificate = CertificateUtil.Load(
                 $"{Environment.WebRootPath}{Configuration["Saml2:SigningCertificateFile"]}",
                 Configuration["Saml2:SigningCertificatePassword"]);
            configuration.AllowedAudienceUris.Add(configuration.Issuer);

            var entityDescriptor = new EntityDescriptor();
                entityDescriptor.ReadIdPSsoDescriptorFromFile(Configuration["Saml2:IdpMetadata"]);

            if (entityDescriptor.IdPSsoDescriptor == null) throw new Exception("Failed to read the metadata.");

            configuration.SignAuthnRequest = true;
            configuration.SingleSignOnDestination = entityDescriptor.IdPSsoDescriptor.SingleSignOnServices
               .Where(ed => ed.Binding.ToString() == "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST")
               .First().Location;
            configuration.SignatureValidationCertificates.AddRange(entityDescriptor.IdPSsoDescriptor.SigningCertificates);
        }); 
  • 棘手的部分来了;默认情况下,sso 启动会使用 RedirectBinding 进行请求,因此会向 sso 服务发送 GET 请求。但是,我尝试使用的服务需要 SAMLRequest 作为 POST 请求。因此,我通过启动 PostBinding 请求更改了代码,然后直接提交表单,如下所示:
    public IActionResult Initiate([FromQuery(Name = "returnUrl")] string returnUrl = "")
    {
        var binding = new Saml2PostBinding();
            binding.SetRelayStateQuery(new Dictionary<string, string> { { "ReturnUrl", returnUrl } });
            binding.Bind(new Saml2AuthnRequest(_saml2configuration)
            {
                ForceAuthn = false,
                IsPassive = false,
                NameIdPolicy = new NameIdPolicy() { AllowCreate = true },
                AssertionConsumerServiceUrl = new Uri("https://localhost:44366/api/Authentication/Process"),
            });

        return binding.ToActionResult();
    } 

问题:
但是,在将 base64 编码的 AuthnRequest 作为 SAML 请求发送后,我收到来自第三方登录的 403 Forbidden。在这个阶段,我不确定是身份提供者配置不正确还是我的请求缺少某些东西。我究竟做错了什么?

以下是(匿名制作的)请求标头。
假设 SAMLRequest 在 formdata 中以 base64 编码形式提供。

    :authority: myEntityDescriptorName
    :method: POST
    :path: mySsoURL
    :scheme: https
    accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
    accept-encoding: gzip, deflate, br
    accept-language: nl-NL,nl;q=0.9,en-US;q=0.8,en;q=0.7
    cache-control: no-cache
    content-length: 3582
    content-type: application/x-www-form-urlencoded
    cookie: JSESSIONID=3D5FE88D55674C2F1E3646E6D8A0FFBE
    origin: https://localhost:44366
    pragma: no-cache
    referer: https://localhost:44366/
    sec-fetch-mode: navigate
    sec-fetch-site: cross-site
    sec-fetch-user: ?1
    upgrade-insecure-requests: 1
    user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36
4

1 回答 1

1

如果需要,将 Authn 请求更改为 Post 绑定是正确的。

您的应用程序是一个服务提供者(也称为依赖方),需要在身份提供者处配置一个唯一的颁发者名称。

我认为问题在于您配置的颁发者名称 ( "Issuer": "myIssuerName") 不正确。颁发者名称应该是您的服务提供者颁发者名称,而不是 metadata.xml 中的身份提供者颁发者名称。

于 2019-11-21T09:28:19.400 回答