从OneLogin验证Saml响应

时间:2017-12-04 18:33:17

标签: c# .net saml onelogin

我试图从OneLogin验证Saml响应并遇到间歇性问题。似乎当InResponseTo中的GUID值以数字开头时,令牌的验证失败并显示异常消息:ID4128:该值不是有效的SAML ID。参数名称:value。但是,当GUID以字母开头时,将成功解析令牌。

我的代码:

 public Saml2SecurityToken GetToken(string token)
    {
        var doc = new XmlDocument();
        doc.LoadXml(token);
        var assertionList = doc.GetElementsByTagName("Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");

        var handlers = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers;
        var securityTokenXml = assertionList[0].OuterXml;

        using (var stringReader = new StringReader(securityTokenXml))
        {
            using (var xmlreader = XmlReader.Create(stringReader))
            {
                return (Saml2SecurityToken) handlers.ReadToken(xmlreader);
            }
        }
    }

我的代币:

<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="2.0" ID="pfx74145d78-55a9-dd78-00a0-2ee76d06a900" IssueInstant="2017-12-04T17:46:32Z">
<saml:Issuer>https://app.onelogin.com/saml/123</saml:Issuer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
        <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
        <ds:Reference URI="#pfx74145d78-55a9-dd78-00a0-2ee76d06a900">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
            <ds:DigestValue>111</ds:DigestValue>
        </ds:Reference>
    </ds:SignedInfo>
    <ds:SignatureValue>abc...</ds:SignatureValue>
    <ds:KeyInfo>
        <ds:X509Data>
            <ds:X509Certificate>abc...</ds:X509Certificate>
        </ds:X509Data>
    </ds:KeyInfo>
</ds:Signature>
<saml:Subject>
    <saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">nameHere</saml:NameID>
    <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
        <saml:SubjectConfirmationData NotOnOrAfter="2017-12-04T17:49:32Z" Recipient="http://localhost/" InResponseTo="7ee0ebbd-3827-4e53-adc5-b8ec35917f2d" />
    </saml:SubjectConfirmation>
</saml:Subject>
<saml:Conditions NotBefore="2017-12-04T17:43:32Z" NotOnOrAfter="2017-12-04T17:49:32Z">
    <saml:AudienceRestriction>
        <saml:Audience>AudienceName</saml:Audience>
    </saml:AudienceRestriction>
</saml:Conditions>
<saml:AuthnStatement AuthnInstant="2017-12-04T17:46:31Z" SessionNotOnOrAfter="2017-12-05T17:46:32Z" SessionIndex="_fd124dd0-bb48-0135-5d75-0a8402be19a0">
    <saml:AuthnContext>
        <saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml:AuthnContextClassRef>
    </saml:AuthnContext>
</saml:AuthnStatement>

完整堆栈跟踪:

at System.IdentityModel.Tokens.Saml2Id..ctor(String value)
at System.IdentityModel.Tokens.Saml2SecurityTokenHandler.ReadSubjectConfirmationData(XmlReader reader)
at System.IdentityModel.Tokens.Saml2SecurityTokenHandler.ReadSubjectConfirmationData(XmlReader reader)
at System.IdentityModel.Tokens.Saml2SecurityTokenHandler.ReadSubjectConfirmation(XmlReader reader)
at System.IdentityModel.Tokens.Saml2SecurityTokenHandler.ReadSubject(XmlReader reader)
at System.IdentityModel.Tokens.Saml2SecurityTokenHandler.ReadAssertion(XmlReader reader)
at System.IdentityModel.Tokens.Saml2SecurityTokenHandler.ReadToken(XmlReader reader)
at System.IdentityModel.Tokens.SecurityTokenHandlerCollection.ReadToken(XmlReader reader)

还有其他方法可以阅读/验证吗?或者可以以某种方式忽略/删除InResponse?

谢谢

1 个答案:

答案 0 :(得分:3)

如果OneLogin发送带有以数字开头的ID的未经请求的响应,则它们不符合规范。 ID具有xsd:ID数据类型并具有特定定义:它必须以字母或下划线开头,并且只能包含字母,数字,下划线,连字符和句点。

但是,看看这个,他们会在InResponseTo字段中发送它...在你的AuthnRequest中发送的的ID是什么......所以,你需要修改你的代码,因为他们很好,只是接受你发送给他们的东西。你当然应该解决这个问题,以便你顺从。