Professional Documents
Culture Documents
How To - Sign XML Documents With Digital Signatures - Microsoft Docs
How To - Sign XML Documents With Digital Signatures - Microsoft Docs
How To - Sign XML Documents With Digital Signatures - Microsoft Docs
In this article
Example
Compiling the Code
.NET Security
See also
7 Note
The code example in this procedure demonstrates how to digitally sign an entire XML
document and attach the signature to the document in a < Signature > element. The
example creates an RSA signing key, adds the key to a secure key container, and then uses
the key to digitally sign an XML document. The key can then be retrieved to verify the XML
digital signature, or can be used to sign another XML document.
For information about how to verify an XML digital signature that was created using this
procedure, see How to: Verify the Digital Signatures of XML Documents.
C# = Copy
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures 1/6
19/11/2020 How to: Sign XML Documents with Digital Signatures | Microsoft Docs
C# = Copy
3. Create an XmlDocument object by loading an XML file from disk. The XmlDocument
object contains the XML element to encrypt.
C# = Copy
4. Create a new SignedXml object and pass the XmlDocument object to it.
C# = Copy
C# = Copy
signedXml.SigningKey = rsaKey;
6. Create a Reference object that describes what to sign. To sign the entire document,
set the Uri property to "" .
C# = Copy
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures 2/6
19/11/2020 How to: Sign XML Documents with Digital Signatures | Microsoft Docs
C# = Copy
C# = Copy
signedXml.AddReference(reference);
C# = Copy
signedXml.ComputeSignature();
10. Retrieve the XML representation of the signature (a < Signature > element) and save it
to a new XmlElement object.
C# = Copy
C# = Copy
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature,
true));
C# = Copy
xmlDoc.Save("test.xml");
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures 3/6
19/11/2020 How to: Sign XML Documents with Digital Signatures | Microsoft Docs
Example
This example assumes that a file named test.xml exists in the same directory as the
compiled program. You can place the following XML into a file called test.xml and use it
with this example.
XML = Copy
<root>
<creditcard>
<number>19834209</number>
<expiry>02/02/2002</expiry>
</creditcard>
</root>
C# = Copy
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures 4/6
19/11/2020 How to: Sign XML Documents with Digital Signatures | Microsoft Docs
SignXml(xmlDoc, rsaKey);
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures 5/6
19/11/2020 How to: Sign XML Documents with Digital Signatures | Microsoft Docs
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature,
true));
}
}
.NET Security
Never store or transfer the private key of an asymmetric key pair in plaintext. For more
information about symmetric and asymmetric cryptographic keys, see Generating Keys for
Encryption and Decryption.
Never embed a private key directly into your source code. Embedded keys can be easily
read from an assembly using the Ildasm.exe (IL Disassembler) or by opening the assembly
in a text editor such as Notepad.
See also
Cryptography Model
Cryptographic Services
Cross-Platform Cryptography
System.Security.Cryptography.Xml
How to: Verify the Digital Signatures of XML Documents
ASP.NET Core Data Protection
Yes No
https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-sign-xml-documents-with-digital-signatures 6/6