如何在C#中为Signature元素添加前缀 [英] How to add prefix to Signature element in C#

查看:328
本文介绍了如何在C#中为Signature元素添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们使用SignedXml类签署xml元素。要求是在Ws Security中添加签名节点以与服务进行通信。



我们能够签署元素并验证这些元素。在这种情况下,xml示例代码如下所示



 X509Certificate2 certificate = new X509Certificate2(CertPath,CertPassword); 

//创建一个新的XML文档。
XmlDocument doc = new XmlDocument();

//格式化文档以忽略空格。
doc.PreserveWhitespace = false;

//使用它的名称加载传递的XML文件。
doc.Load(new XmlTextReader(FileName));

SignedXml signedXml = new SignedXml(doc);
signedXml.SigningKey = certificate.PrivateKey;


//创建要签名的引用。
参考reference = new Reference();

reference.Uri ="#" + elementID;

//为参考添加包络变换。
XmlDsigEnvelopedSignatureTransform envTransform = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(envTransform);

//添加对SignedXml对象的引用。
signedXml.AddReference(reference);



//创建一个新的KeyInfo对象。
KeyInfo keyInfo = new KeyInfo();

//将证书加载到KeyInfoX509Data对象
//并将其添加到KeyInfo对象。
keyInfo.AddClause(new KeyInfoX509Data(certificate));

//将KeyInfo对象添加到SignedXml对象。
signedXml.KeyInfo = keyInfo;

//计算签名。
signedXml.ComputeSignature();


//获取签名的XML表示并将
//保存到XmlElement对象。
XmlElement xmlDigitalSignature = signedXml.GetXml();





使用此Signature元素生成为< signature>
....< / signature>
 ,但我们希望将其生成为< ds:signature>
....< / ds:signature>



试图明确设置前缀,但之后签名没有验证。



你能指导我们如何实现这个目标吗?



解决方案

我建议你发帖到下面的论坛。


http://forums.asp.net/28.aspx/1?WCF+ASMX+和其他+ + +的Web服务


We are signing xml elements using SignedXml class. The requirement is add signature node in Ws Security to communicate with the service.

We could able to sign the elements and verified those. In this case the xml Sample code is as below

X509Certificate2 certificate = new X509Certificate2(CertPath, CertPassword);

    // Create a new XML document.
    XmlDocument doc = new XmlDocument();

    // Format the document to ignore white spaces.
    doc.PreserveWhitespace = false;

    // Load the passed XML file using it's name.
    doc.Load(new XmlTextReader(FileName));

    SignedXml signedXml = new SignedXml(doc);
    signedXml.SigningKey = certificate.PrivateKey;


    // Create a reference to be signed.
    Reference reference = new Reference();

    reference.Uri = "#" + elementID;

    // Add an enveloped transformation to the reference.
     XmlDsigEnvelopedSignatureTransform envTransform = new XmlDsigEnvelopedSignatureTransform();
    reference.AddTransform(envTransform);

    // Add the reference to the SignedXml object.
    signedXml.AddReference(reference);



    // Create a new KeyInfo object.
    KeyInfo keyInfo = new KeyInfo();

    // Load the certificate into a KeyInfoX509Data object
    // and add it to the KeyInfo object.
    keyInfo.AddClause(new KeyInfoX509Data(certificate));

    // Add the KeyInfo object to the SignedXml object.
    signedXml.KeyInfo = keyInfo;

    // Compute the signature.
    signedXml.ComputeSignature();


    // Get the XML representation of the signature and save
    // it to an XmlElement object.
    XmlElement xmlDigitalSignature = signedXml.GetXml();


by using this Signature element generated as <signature> .... </signature> , but we want to generate it as <ds:signature> .... </ds:signature>

Tried to set the prefix explicitly but then signature is not validated after.

Could you please guide how can we achieve this?

解决方案

I suggest that you post to the forum below.

http://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services


这篇关于如何在C#中为Signature元素添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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