簡體   English   中英

如何在C#中為Signature元素添加前綴

[英]How to add prefix to Signature element in C#

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

嘗試顯式設置前綴,但之后未驗證簽名。

您能否指導我們如何實現這一目標?

由於這是一個足夠籠統的問題, 因此該帖子可能對尋求相同信息的人有所幫助

對於我正在處理的特定項目,IRS A2A提交,這是在我認為需要將前綴添加到Signature元素時使用的。 但是,在與其他SO用戶進行討論時,我放棄了將前綴添加到Signature元素的想法,因為實際上並沒有必要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM