1

我正在查看有关在 C# 中验证 Java 生成的 SAML 响应的各种帖子。我一直在尝试所有的建议,但仍然得到一个错误的SignedXml.CheckSignature结果,现在我完全不知道可能出了什么问题,我正在与你们联系,看看你们是否有我可以使用的建议。

响应中的签名节点是

 <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  <ds:SignedInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
   <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
   <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
   <ds:Reference URI="#SM16afb708b851b15451d92108ac8c6a2a627a2643667" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:Transforms xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
     <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
    </ds:Transforms>
    <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" xmlns:ds="http://www.w3.org/2000/09/xmldsig#"/>
    <ds:DigestValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">kOlL02M8icLI1MtFnFUAcf/yols=</ds:DigestValue>
   </ds:Reference>
  </ds:SignedInfo>
  <ds:SignatureValue xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
   d2wjnwPdzDrsWvMq9EElkb0TVsj8LmMXqTusPuG3GbxsqVLFryqaMkwv/whTYD/evw8WNClJm1nC
   VpGTEPEB/voPiFJaNpdwvs6a6PuTizLQQaqOC1H6JC6KboUVR87wuJ4kV3W9QoGEft+OmZXMgUU6
   54PgOX3d/czqlQWS9Z8=
  </ds:SignatureValue>
  <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
   <ds:X509Data xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
    <ds:X509Certificate xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
     MIICCjCCAXOgAwIBAgIQazR4XEyfFItIKZvB/IC3/jANBgkqhkiG9w0BAQQFADATMREwDwYDVQQD
     EwhTYW1sVGVzdDAgFw0wMDAxMDEwNDAwMDBaGA8yMDk5MDEwMTA0MDAwMFowEzERMA8GA1UEAxMI
     U2FtbFRlc3QwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAK4kfW9Jg/WtQ+4yy631r2qkCVyK
     odGL0A3lg+4w3BfCSGf4N7GkAlHI4G582tC4tlwd/mj/IvN6qxhIyu45OlENZhWOXFOCogoX7Tfx
     Vd7XRUqNwdndjy9KY7uyIrXVczpGbk+ahp6SS0NAG9i1rYR/pxTvW4zUHkrFbgm8gdghAgMBAAGj
     XTBbMBMGA1UdJQQMMAoGCCsGAQUFBwMDMEQGA1UdAQQ9MDuAEHITUeo8fKKVys7DcdV65hmhFTAT
     MREwDwYDVQQDEwhTYW1sVGVzdIIQazR4XEyfFItIKZvB/IC3/jANBgkqhkiG9w0BAQQFAAOBgQA/
     Dm+yk4K3q8AG3q4dorWswL8fwU9dpjuvheRjAveaL5kr59QRanG+lLi8Wefg6iTPKDgIGc2VG13T
     KP7pZSysaXJ5i8N0zCg+eu/YL7Hw1kSXW/CdxwQ+qI1W53fQ9NXlLVWPXhIepAOL46EEZgACHEhv
     tG30XtStycyLOsgm9A==
    </ds:X509Certificate>
   </ds:X509Data>
  </ds:KeyInfo>
 </ds:Signature>

我用来验证响应的 C# 代码是

try
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.PreserveWhitespace = true;
    xmlDoc.Load(@"C:\SAMLSSOResponse.txt");

    XmlNamespaceManager _documentNamespaceManager;
    _documentNamespaceManager = new XmlNamespaceManager(xmlDoc.NameTable);
    _documentNamespaceManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
    _documentNamespaceManager.AddNamespace("samlp", "urn:oasis:names:tc:SAML:1.0:protocol");
    _documentNamespaceManager.AddNamespace("saml", "urn:oasis:names:tc:SAML:1.0:assertion");

    SignedXml signedXml = new SignedXml(xmlDoc);
    XmlNodeList nodeList = xmlDoc.GetElementsByTagName("ds:Signature");

    XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode("/samlp:Response/ds:Signature", _documentNamespaceManager);
    //xmlDoc.GetElementsByTagName("Signature");
    //signedXml.LoadXml((XmlElement)xmlNode);
    signedXml.LoadXml((XmlElement)nodeList[0]);

    X509Certificate2 certificate = null;
    foreach (KeyInfoClause clause in signedXml.KeyInfo)
    {
        if (clause is KeyInfoX509Data)
        {
            if (((KeyInfoX509Data)clause).Certificates.Count > 0)
            {
                certificate = (X509Certificate2)((KeyInfoX509Data)clause).Certificates[0];
            }
        }
    }

    if (certificate == null)
    {
        Console.WriteLine("No Certificate found");
    }

    Console.WriteLine("Testing with Certificate in the XML");
    if (signedXml.CheckSignature(certificate, true))
        Console.WriteLine("Validated");
    else
        Console.WriteLine("Failed");
}
catch (Exception ex)
{
}

我什至尝试通过在代码中加载证书来验证签名,但仍然遇到同样的问题。

有人对为什么签名没有得到验证有任何建议吗?

4

1 回答 1

0

我解决了这个问题。我必须处理多个问题:

  1. 我正在处理的响应是一个我得到但没有验证的字符串。当我得到 Base64Encoded 字符串然后尝试验证它时,它给了我一个不同的错误(格式错误的参考元素),该错误已通过步骤 2 解决。

  2. 通过创建从 SignedXML 继承的新类,然后覆盖 GetIdElement 类,解决了格式错误的引用元素问题。这是代码:

公共类 SamlSignedXml : SignedXml
    {
        私有字符串 _referenceAttributeId = "";
        公共 SamlSignedXml(XmlElement 元素,字符串 referenceAttributeId)
            :基础(元素)
        {
            _referenceAttributeId = referenceAttributeId;
        }
        公共覆盖 XmlElement GetIdElement(
            XmlDocument 文档,字符串 idValue)
        {
            返回(XmlElement)
                文档.SelectSingleNode(
                    string.Format("//*[@{0}='{1}']",
                    _referenceAttributeId, idValue));
        }
    }

当您创建 SamlSignedXml 类的实例时,您可以执行以下操作: SamlSignedXml signedXml = new SamlSignedXml(element, "ResponseID");

其中 element 是 XmlDocument.DocumentElement。

这解决了问题

于 2010-11-05T14:37:46.897 回答