Export (0) Print
Expand All

SignedXml.AddObject Method

Adds a DataObject object to the list of objects to be signed.

Namespace:  System.Security.Cryptography.Xml
Assembly:  System.Security (in System.Security.dll)

public void AddObject(
	DataObject dataObject
)

Parameters

dataObject
Type: System.Security.Cryptography.Xml.DataObject

The DataObject object to add to the list of objects to be signed.

The AddObject method adds an <Object> element that represents an object to be signed to the <Signature> element of an XML digital signature.

The AddObject method internally calls the AddObject method of the Signature object encapsulated by the SignedXml object. You can also add a DataObject object by directly calling the AddObject method from the Signature property.

For more information about XML digital signatures, see the XMLDSIG specification available at www.w3.org/TR/xmldsig-core/.

The following code example computes and XML signature.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;


public class XMLdsigsample1 
{

    static void Main(String[] args)
    {
        try
        {
            // Create example data to sign.
            XmlDocument document = new XmlDocument();
            XmlNode node = document.CreateNode(XmlNodeType.Element, "", "MyElement", "samples");
            node.InnerText = "This is some text";
            document.AppendChild(node);
            Console.WriteLine("Data to sign:\n" + document.OuterXml + "\n");

            // Create the SignedXml message.
            SignedXml signedXml = new SignedXml();
            RSA key = RSA.Create();
            signedXml.SigningKey = key;

            // Create a data object to hold the data to sign.
            DataObject dataObject = new DataObject();
            dataObject.Data = document.ChildNodes;
            dataObject.Id = "MyObjectId";

            // Add the data object to the signature.
            signedXml.AddObject(dataObject);

            // Create a reference to be able to package everything into the 
            // message.
            Reference reference = new Reference();
            reference.Uri = "#MyObjectId";

            // Add the reference to the message.
            signedXml.AddReference(reference);

            // Add a KeyInfo.
            KeyInfo keyInfo = new KeyInfo();
            keyInfo.AddClause(new RSAKeyValue(key));
            signedXml.KeyInfo = keyInfo;

            // Compute the signature.
            signedXml.ComputeSignature();

            Console.WriteLine("The data was signed.");
        }
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft