【问题标题】:How to add prefix to Signature element in C#如何在 C# 中为 Signature 元素添加前缀
【发布时间】:2016-03-02 12:45:39
【问题描述】:

我们正在使用 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>

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

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

【问题讨论】:

    标签: c# ws-security x509certificate2 xml-signature signedxml


    【解决方案1】:

    因为这是一个足够笼统的问题,this post might be helpful to someone looking for the same information

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

    【讨论】:

    • 感谢您,我们尝试了同样的方法,即在没有前缀的情况下传递签名,但仍然陷入 .NET 中的 WS 安全错误。您是否在 .NET 或任何参考中传递了此错误?
    猜你喜欢
    • 2018-12-05
    • 1970-01-01
    • 2021-09-26
    • 2023-04-02
    • 2013-04-15
    • 2015-07-29
    • 2020-01-27
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多