Wrox Programmer Forums
.NET 4 and Visual Studio 2010 General Discussions For discussing anything about .NET 4, WPF, WCF, the rest of the .NET 4 Framework, and Visual Studio 2010 that isn't about a specific Wrox book. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET 4 and Visual Studio 2010 General Discussions section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 2nd, 2014, 05:54 AM
Registered User
Join Date: Jun 2014
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add a prefix to sign an XML file

I use an X509 certificate to sign an XML file but the prefix related to the signature is missing, I have to add the "ds" one.

How to add this prefix without having to break my signature?

Here is my code:

Dim f_adresse As String = System.AppDomain.CurrentDomain.BaseDirectory
Dim f_temp As String = f_adresse & "TEMP\"
Dim f_fichier As String = "20381235051-01-FF12-03.xml"
Dim f_certificat As String = f_adresse & "aG9CcVpHVndCWTd3WlVOVw==.p12"
Dim f_pwd As String = "GiQ5uTsBZ9gHxzNH"

Dim xmlFile As String = f_temp & f_fichier
Dim MonCertificat As X509Certificate2 = New X509Certificate2(f_certificat, f_pwd)

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.PreserveWhitespace = True

Dim signedXml As SignedXml = New SignedXml(xmlDoc)
signedXml.SigningKey = MonCertificat.PrivateKey
Dim KeyInfo As KeyInfo = New KeyInfo()
Dim Reference As Reference = New Reference()
Reference.Uri = ""

Reference.AddTransform(New XmlDsigEnvelopedSignatureTransform("true"))


Dim X509Chain As X509Chain = New X509Chain()

Dim local_element As X509ChainElement = X509Chain.ChainElements(0)
Dim x509Data As KeyInfoX509Data = New KeyInfoX509Data(local_element.Certificate)
Dim subjectName As String = local_element.Certificate.Subject


signedXml.KeyInfo = KeyInfo

Dim signature As XmlElement = signedXml.GetXml()

signature.Prefix = "ds"
signature.ChildNodes(0).Prefix = "ds" 'this line breaks my signature

For Each node As XmlNode In signature.SelectNodes("descendant-or-self::*[namespace-uri()='http://www.w3.org/2000/09/xmldsig#']")
    If node.LocalName = "Signature" Then
        Dim newAttribute As XmlAttribute = xmlDoc.CreateAttribute("Id")
        newAttribute.Value = "SignatureSP"
    End If
Next node

Dim nsMgr As XmlNamespaceManager
nsMgr = New XmlNamespaceManager(xmlDoc.NameTable)
nsMgr.AddNamespace("sac", "urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1")
nsMgr.AddNamespace("ccts", "urn:un:unece:uncefact:documentation:2")
nsMgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
nsMgr.AddNamespace("tns", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2")
nsMgr.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
nsMgr.AddNamespace("udt", "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2")
nsMgr.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
nsMgr.AddNamespace("qdt", "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2")
nsMgr.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
nsMgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")

xmlDoc.SelectSingleNode("/tns:Invoice/ext:UBLExtensions/ext:UBLExtension[2]/ext:ExtensionContent", nsMgr).AppendChild(xmlDoc.ImportNode(signature, True))

'check the signature
Dim nodeList As XmlNodeList = xmlDoc.GetElementsByTagName("ds:Signature")
If nodeList.Count <= 0 Then
     MsgBox("Verification failed: No Signature was found in the document.")
     Exit Sub
End If

If nodeList.Count >= 2 Then
     MsgBox("Verification failed: More that one signature was found for the document.")
     Exit Sub
End If
signedXml.LoadXml(CType(nodeList(0), XmlElement))
msgbox("Check Signature : " & signedXml.CheckSignature())

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a prefix to sign an XML file romulus001 Visual Basic 2010 General Discussion 0 July 2nd, 2014 05:03 AM
how to add comment in a java file and xml GFreedom BOOK: Beginning Android Application Development 1 October 7th, 2011 08:12 AM
How do I add to an XML file using VB? QuadFather XML 4 April 6th, 2010 02:32 PM
Adding Prefix and Namespace to XML Lerxt XSLT 6 November 8th, 2007 03:14 PM
Create XML file - According to the Value, add elem remya1000 General .NET 0 October 2nd, 2007 11:03 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.