【发布时间】:2022-01-17 17:24:13
【问题描述】:
我在一个文件中有这个 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 对象上使用该方法,但效果不一样。我做错了什么?
谢谢
【问题讨论】:
-
你有在这里使用
XmlReader和XPathSelectElement吗?有使用 LINQ to XML 获取正确元素的更简单方法。请注意,您要的是/Signature,但根元素是 AuthnRequest,然后是其中的 Signature。也许这就是问题所在? -
我只是使用了 / 为什么它应该在 AuthnRequest 下下降一个节点,但无论如何即使指定 AuthnRequest / Signature 也不起作用
-
好的,关于您是否有使用 XPathSelectElement 的问题呢?如果您对使用
Element方法感到满意,那么这很简单。 -
我现在正在使用其他方法,但我真的很好奇为什么它不起作用
-
好的,如果您的问题是特定于使用 NamespaceManager,请修复示例以包含 /AuthnRequest。但我怀疑docs.microsoft.com/en-us/dotnet/api/… 中的注释在这里是相关的——我怀疑你应该指定一个非空前缀。