2

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

我们可以对元素进行签名并对其进行验证。在这种情况下,xml示例代码如下

        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();

通过使用生成为 < 的此 Signature 元素signature> .... </signature>。但我们想将其生成为 <ds:signature> .... </ds:signature>

尝试显式设置前缀,但之后未验证签名。

您能否指导我们如何实现这一目标?

4

1 回答 1

1

由于这是一个足够通用的问题,因此这篇文章可能对寻找相同信息的人有所帮助

对于我正在处理的特定项目,IRS A2A 提交,这是我在认为我需要向Signature元素添加前缀时使用的。但是,在与其他一些 SO 用户的讨论中,我放弃了向Signature元素添加前缀的想法,因为它实际上并不是必需的。

于 2016-03-23T22:36:33.883 回答