2016-03-02 59 views
1

我們正在使用SignedXml類簽署xml元素。要求是在Ws Security中添加簽名節點來與服務進行通信。如何在C#中爲Signature元素添加前綴

我們可以簽署元素並驗證這些元素。在此情況下的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>產生此簽名元素。但我們想要生成它< ds:signature> .... </ds:signature>

試圖明確設置前綴,但簽名後未驗證。

請問您能指導我們如何做到這一點?

回答

0

由於這是一個足夠普遍的問題,this post might be helpful to someone looking for the same information

對於我正在處理的特定項目,IRS A2A提交,這是我在考慮需要將前綴添加到Signature元素時所使用的項目。但是,在與其他一些SO用戶的討論中,我放棄了爲Signature元素添加前綴的想法,因爲它實際上並非必要。

+0

感謝這一點,我們嘗試了同樣的事情,即傳遞無前綴簽名,但仍然陷入.NET中的WS安全錯誤。你有沒有在.NET或任何參考中傳遞這個錯誤? – Oxygen