c# - 验证来自 OneLogin 的 Saml 响应

标签 c# .net saml onelogin

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

我的代码:

 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);
            }
        }
    }

我的 token :

<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)

是否有另一种方法来读取/验证它?或者可以以某种方式忽略/删除 InResponseTo 吗?

谢谢

最佳答案

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

然而,看看这个,他们在 InResponseTo 字段中发送了那个……在您的 AuthnRequest 中发送给他们的 ID 是什么……所以,您需要修复您的代码,因为他们很好,只是接受您发送给他们的东西。你当然应该解决这个问题,这样你就合规了。

关于c# - 验证来自 OneLogin 的 Saml 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639753/

相关文章:

web-services - 使用 SAML 的 SSO 桌面客户端应用程序

c# - 脚本手动运行,但不通过C#运行

c# - VS 团队测试 : Multiple Test Initialize Methods in Test Class

c# - Entity Framework 5 - 无法加载文件或程序集 EntityFramework,版本 = 5.0.0.0

.net - 默认 .NET 异常处理程序

C# 在 List<T> Count 上等待

oauth-2.0 - 使用 SAML 单点登录获取 OAuth 访问 token 或代码以使用 Box.com API

c# - VS2013 Intellisense 不断停止工作

c# - BindingFlags.InvokeMethod 是什么意思?

Azure Active Directory SCIM 映射