.NET签名XML preFIX [英] .NET Signed XML Prefix

查看:191
本文介绍了.NET签名XML preFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来设置一个签名的XML文档(在.net SignedXml类)?

的签名的preFIX 代替

所以:

 <签名的xmlns =htt​​p://www.w3.org/2000/09/xmldsig#>
...
< /签名>
 

我可能有以下几点:

 < D​​S:签名的xmlns:DS =htt​​p://www.w3.org/2000/09/xmldsig#>
...
< / DS:签名>
 

解决方案

首先,实在是没有什么好的理由这样做。这两种形式的在功能上等同。任何乖巧的XML处理器将处理它们绝对相同。所以,除非你正在试图说服一个不正确地实现XML命名空间的应用程序,这是更好(IMO)只是独自离开默认的形式。 (即使在这种情况下,这将是更好的,如果可能的话,要得到有故障的应用固定代替。)

这就是说,你可以手动设置由SignedXml.GetXml(返回的的XmlElement的preFIX)和使用XPath这样的子元素:

 的XmlElement签名= signedXml.GetXml();
在signature.SelectNodes的foreach(XmlNode的节点(
    后代或自身:: * [命名空间URI()='HTTP://www.w3.org/2000/09/xmldsig#']))
{
    。点preFIX =DS;
}
 

Is there a way to set the prefix on the Signature of a Signed XML Document (SignedXml class in .Net)?

So instead of:

<Signature xmlns="http://www.w3.org/2000/09/xmldsig#>
...
</Signature>

I could have the following:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#>
...
</ds:Signature>

解决方案

First of all, there really isn't any good reason to do this. The two forms are functionally equivalent. Any well-behaved XML processor will handle them absolutely identically. So unless you are trying to talk to an application that doesn't properly implement XML namespaces, it's better (IMO) just to leave the default form alone. (And even in that case, it would be better, if at all possible, to get the faulty application fixed instead.)

That said, you can manually set the prefix on the XmlElement that is returned by SignedXml.GetXml() and its child elements using XPath like this:

XmlElement signature = signedXml.GetXml();
foreach (XmlNode node in signature.SelectNodes(
    "descendant-or-self::*[namespace-uri()='http://www.w3.org/2000/09/xmldsig#']"))
{
    node.Prefix = "ds";
}

这篇关于.NET签名XML preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆