XAdES .NET Project Documentation

XMLDSIG Sign - Single Signature

In this section, we describe XAdES .NET Project complete API to create single XMLDSIG signatures. With the XmlDsigHelper class, it is possible to sign:
  • Xml Files
  • XmlDocument objects

The API to do it is as follows:

	XmlDsigHelper

			// Signature specification methods (return SignDSL)
	
			.Sign(string inputPath)										// Specifies the file to sign
			.Sign(XmlDocument xmlDocument)									// Specifies the input XmlDocument object to sign
			.Using(X509Certificate2 certificate)								// States the certificate to use in the signature
			.Enveloping()											// Signature will have Enveloping format
			.Enveloped()											// Signature will have Enveloped format
			.Detached()											// Signature will have Detached format
			.UsingFormat(XmlDsigSignatureFormat xmlDsigSignatureFormat)					// A way to specify the format using a parameter
			.IncludingCertificateInSignature()								// Forces the signature to include the signer certificate
			.DoNotIncludeCertificateInSignature()								// The signature will not contain the signer certificate
			.IncludeTimestamp(bool includeTimestamp)							// Specifies if the signature will have a property with the timestamp
			.WithProperty(string propertyName, 								
				string propertyValue)									// Adds a Xml property to the signature
			.WithProperty(string propertyName, 
				string propertyValue, string propertyNameSpace)						// Adds a Xml property to the signature
			.WithPropertyBuiltFromDoc(
				Converter<XmlDocument, 
				XmlElement> howToCreatePropertyNodeFromDoc)						// Adds a Xml property to the signature, created dynamically from a XmlDocument
			.NodeToSign(string xpathToNode)									// Specifies the node to sign (if not used, all the document will be signed)

			// Signature retrieval methods (performs the signature and return the value -if needed-)
			
			.void SignToFile(string outputPath)								// Signs and saves the result in outputPath
			.XmlDocument SignAndGetXml()									// Signs and returns signature value as a XmlDocument



Here, you can see some examples:
  • Single signature to a file:
	// ... Previous code

	// Signs input.xml and produces output.xml file
	var myCertificate = ReadMyCertificate(); // ... a certificate is needed to sign, put here the code to access it
	XmlDsigHelper.Sign("input.xml").Using(myCertificate).SignToFile("output.xml");
	
	// ... and the method continues

  • Single enveloping signature to a file:
	// ... Previous code

	XmlDsigHelper.Sign("input.xml").Using(myCertificate).Enveloping().SignToFile("output.xml");
	
	// ... and the method continues

  • Single detached signature to a file, with timestamp:
	// ... Previous code

	XmlDsigHelper.Sign("input.xml").Using(myCertificate).Detached().IncludeTimestamp(true).SignToFile("output.xml");
	
	// ... and the method continues

  • Single detached signature to a file, with custom property node:
	// ... Previous code

	XmlDsigHelper.Sign("input.xml").Using(myCertificate).Detached().IncludeTimestamp(true).WithPropertyBuiltFromDoc(node => CreatePropertyNodeForm(node)).SignToFile("output.xml");
	
	// ... and the method continues (CreatePropertyNodeForm should be implemented)

  • Single enveloped signature (option decided at runtime) to a XmlDocument, only signing node with id = 'hello':
	// ... Previous code
	
	var selectedFormat = XmlDsigSignatureFormat.Enveloped;
	var signedXml = XmlDsigHelper.Sign("input.xml").Using(myCertificate).UsingFormat(selectedFormat).NodeToSign("#hello").SignAndGetXml();
	
	// ... and the method continues

Last edited Nov 23, 2010 at 7:14 AM by lvillap, version 6

Comments

No comments yet.