1

我有一个 Asp.Net Core Web 应用程序,我目前正在使用 Kentor AuthServices 实现 SP 发起的 SSO。现在,我收到了来自 idP 的元数据文件,格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata" 
 xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" 
 entityID="https://exampleidp.com">
<md:IDPSSODescriptor 
 protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
<md:KeyDescriptor use="signing">
   <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
      <X509Data>
         <X509Certificate>ExampleCertificate</X509Certificate>
      </X509Data>
   </KeyInfo>
</md:KeyDescriptor>
<md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified
</md:NameIDFormat>
<md:SingleSignOnService
 Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
 Location="https://exampleidp.com/loginpage"/>
<md:SingleLogoutService 
 Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" 
 Location="https://exampleidp.com/logoutpage"/>
<saml:Attribute Name="accountID" 
 NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" 
 FriendlyName="accountID"/>
<saml:Attribute Name="email" 
 NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" 
 FriendlyName="email"/>
<saml:Attribute Name="firstName" 
 NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" 
 FriendlyName="firstName"/>
<saml:Attribute Name="lastName" 
 NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic" 
 FriendlyName="lastName"/> 
</md:IDPSSODescriptor>
<md:ContactPerson contactType="technical">
   <md:GivenName>Exampleidp</md:GivenName>
   <md:SurName>Support</md:SurName>
   <md:EmailAddress>exampleidp.support@exampleidp.com</md:EmailAddress>
</md:ContactPerson>
<md:Organization>
   <md:OrganizationName xml:lang="en">Example Idp</md:OrganizationName>
   <md:OrganizationDisplayName xml:lang="en">Example Idp</md:OrganizationDisplayName>
   <md:OrganizationURL xml:lang="en">http://exampleidp.com/</md:OrganizationURL>
</md:Organization>  
</md:EntityDescriptor>

以及两个用于签名的证书。在我的Startup.cs中,我在 ConfigureServices 中添加了以下代码片段:

.AddSaml2(options =>
        {
            options.SPOptions.EntityId = new EntityId("http://myapp.com");
            options.IdentityProviders.Add(
                new IdentityProvider(
                    new EntityId("myapp.com/Metadata.xml"), options.SPOptions)
                {
                    LoadMetadata = true
                });
        })

以及配置中的以下代码片段:

app.UseAuthentication();

更新

在查看了更多源代码之后,我终于能够转过头来生成如下所示的 AuthnRequest(我使用 idP 进行了验证):

<saml2p:AuthnRequest xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" 
 xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" 
 ID="idcc70905185a04a94b05282e0c544e086" Version="2.0" IssueInstant="2017-
 11-13T14:47:56Z" 
 ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" 
 AssertionConsumerServiceURL="http://myapp.com/saml">
    <saml2:Issuer>http://myapp.com</saml2:Issuer>
    <saml2p:NameIDPolicy Format="urn:oasis:names:tc:SAML:1.1:nameid-
     format:unspecified" AllowCreate="true" />
</saml2p:AuthnRequest>

编辑:我现在主要关心的是我需要如何将 AuthnRequest 发送到 idP。看来,如果我走在正确的轨道上,我需要对 AuthnRequest 进行放气和编码,以便 idP 正确处理它。我想知道如何使用现有的 Kentor 代码来实现这一点(看起来我可能需要包含 RelayState 信息?)或者可能很容易自己做?

4

1 回答 1

0

最终更新:经过大量研究和四处探索,我终于能够解决我的最后一个问题,即将 AuthnRequest 设置为需要发送到 idP 的适当的放气和 base64 编码的字符串。我使用以下代码块实现了这一点:

var param = "";
        var bytes = Encoding.UTF8.GetBytes(authnRequest.ToXElement().ToString());
        using (var output = new MemoryStream())
        {
            using (var zip = new DeflateStream(output, CompressionMode.Compress))
            {
                zip.Write(bytes, 0, bytes.Length);
            }
            var base64String = Convert.ToBase64String(output.ToArray());
            param =  HttpUtility.UrlEncode(base64String);
        }

这触发了 idP 的正确响应,因此我现在能够为我的 SP 正确触发 SSO 并从 idP 获取响应值。

于 2017-11-14T18:32:04.280 回答