由Saml2PostBinding发起SingleSignOn

问题描述 投票:4回答:1

将ITfoxtec用于ASP.NET Core 3.0时出现问题。作为上下文,我试图在Web应用程序和第三方登录服务之间建立连接。为了预先封装一些可能性,第三方可以访问我们的元数据URL并为我们的Web应用程序配置其服务。

所需的用户工作流程:

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

到目前为止采取的步骤:

  • 在appsettings.json中添加了Saml2部分,其中包含我们的metas.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禁止访问。在此阶段,我不确定身份提供程序是否配置正确或我的请求是否缺少某些内容。我究竟做错了什么?下面是(匿名生成的)请求标头。假定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
post saml-2.0 asp.net-core-3.0 itfoxtec-identity-saml2
1个回答
1
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.