gpt4 book ai didi

c# - 在 C# 中验证 Java SAML 响应

转载 作者:行者123 更新时间:2023-12-01 05:52:47 24 4
gpt4 key购买 nike

我正在查看有关验证 Java 在 C# 中生成的 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)
{
}

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

有人对签名未得到验证的原因有任何建议吗?

最佳答案

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

  1. 我正在处理的响应是我收到的但未验证的字符串。当我获得 Base64Encoded 字符串并尝试验证它时,出现了一个不同的错误(格式错误的引用元素),该错误已通过步骤 2 解决。

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

public class SamlSignedXml : SignedXml    {        private string _referenceAttributeId = "";        public SamlSignedXml(XmlElement element, string referenceAttributeId)            : base(element)        {            _referenceAttributeId = referenceAttributeId;        }        public override XmlElement GetIdElement(            XmlDocument document, string idValue)        {            return (XmlElement)                document.SelectSingleNode(                    string.Format("//*[@{0}='{1}']",                    _referenceAttributeId, idValue));        }    }

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

其中 element 是 XmlDocument.DocumentElement。

问题解决了

关于c# - 在 C# 中验证 Java SAML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4082045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com