我在一個檔案中有這個 xml 檔案:
<samlp:AuthnRequest xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_73c4b43a-d201-4990-b656-e6bab19e1c09" Version="2.0" IssueInstant="2021-12-14T08:09:39.816485Z" Destination="https://localhost/idp/sso/post" ForceAuthn="true" AssertionConsumerServiceIndex="0" AssertionConsumerServiceURL="https://localhost:5002/signin-spid" AttributeConsumingServiceIndex="0" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" >
<saml:Issuer NameQualifier="https://localhost:5002" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity" >https://localhost:5002</saml:Issuer>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
<Reference URI="#_73c4b43a-d201-4990-b656-e6bab19e1c09">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
<DigestValue>.........DigestValue...........</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>..............Signature................</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>...........Certificate.............</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
<samlp:NameIDPolicy Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" />
<saml:Conditions NotBefore="2021-12-14T07:59:39.816485Z" NotOnOrAfter="2021-12-14T08:19:39.816485Z" />
</samlp:AuthnRequest>
我只是想使用以下代碼獲取 SignatureValue 標記值和 X509Certificate 標記值:
var xDocument = XDocument.Load("Request.xml");
var reader = xDocument.CreateReader();
var namespaceManager = new XmlNamespaceManager(reader.NameTable);
namespaceManager.AddNamespace("", "http://www.w3.org/2000/09/xmldsig#");
var signature = xDocument.XPathSelectElement("/Signature/SignatureValue", namespaceManager);
var x509Certificate = xDocument.XPathSelectElement("/Signature/KeyInfo/X509Data/X509Certificate", namespaceManager);
但 Signature 和 x509Certificate 元素始終為空。我也嘗試直接在 Root 物件上使用該方法,但它不起作用。我究竟做錯了什么?
謝謝
uj5u.com熱心網友回復:
最好使用 LINQ to XML API。
自 2007 年以來,它在 .Net Framework 中可用。
C#
void Main()
{
const string filename = @"e:\Temp\pampua84.xml";
XDocument xdoc = XDocument.Load(filename);
XNamespace ns = "http://www.w3.org/2000/09/xmldsig#";
string SignatureValue = xdoc.Descendants(ns "SignatureValue")
.FirstOrDefault()?.Value;
string X509Certificate = xdoc.Descendants(ns "X509Certificate")
.FirstOrDefault()?.Value;
Console.WriteLine("SignatureValue='{0}'", SignatureValue);
Console.WriteLine("X509Certificate='{0}'", X509Certificate);
}
uj5u.com熱心網友回復:
這是一個快速的。您必須為空的默認命名空間提供前綴。它可以是任何東西,在這個例子中我稱之為“root”。
using System.Xml;
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("root", "http://www.w3.org/2000/09/xmldsig#");
XmlDocument xml = new XmlDocument();
xml.Load("c:\\temp\\test.xml");
XmlNode ndSignature = xml.SelectSingleNode("//root:Signature/root:SignatureValue", nsmgr);
XmlNode ndCertificate = xml.SelectSingleNode("//root:Signature/root:KeyInfo/root:X509Data/root:X509Certificate", nsmgr);
Console.WriteLine(ndSignature.InnerText);
Console.WriteLine(ndCertificate.InnerText);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383954.html
下一篇:如何在XML中查找節點
