Export (0) Print
Expand All

DataObject Class

Represents the object element of an XML signature that holds data to be signed.

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

public class DataObject
public class DataObject
public class DataObject

Use the DataObject class to store information or metadata directly in an XML signature. For example, you can store the signature generation date or the signer's identity. The DataObject class may or may not be covered by the XML signature.

The class corresponds to the <Object> element in the World Wide Web Consortium (W3C) specification for XML Signatures. For more information about the W3C specification, see http://www.w3.org/TR/xmldsig-core/.

The following code example demonstrates how to generate an enveloping 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)
{
     // 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.Error.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 it 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();

     // Get the XML representation of the signature.
     XmlElement xmlSignature = signedXml.GetXml();
     Console.WriteLine(xmlSignature.OuterXml);
}

}

import System.*;
import System.IO.*;
import System.Security.Cryptography.*;
import System.Security.Cryptography.Xml.*;
import System.Xml.*;

public class XmlDsigSample1
{
    public static void main(String[] args)
    {
        // Create example data to sign.
        XmlDocument document = new XmlDocument();
        XmlNode node = document.CreateNode(XmlNodeType.Element, "",
            "MyElement", "samples");
        node.set_InnerText("This is some text");
        document.AppendChild(node);
        Console.get_Error().WriteLine("Data to sign:\n" +
            document.get_OuterXml() + "\n");
        // Create the SignedXml message.
        SignedXml signedXml = new SignedXml();
        RSA key = RSA.Create();
        signedXml.set_SigningKey(key);
        // Create a data object to hold the data to sign.
        DataObject dataObject = new DataObject();
        dataObject.set_Data(document.get_ChildNodes());
        dataObject.set_Id("MyObjectId");
        // Add the data object to the signature.
        signedXml.AddObject(dataObject);
        // Create a reference to be able to
        Reference reference = new Reference();
        reference.set_Uri("#MyObjectId");
        // Add it to the message.
        signedXml.AddReference(reference);
        // Add a KeyInfo.
        KeyInfo keyInfo = new KeyInfo();
        keyInfo.AddClause(new RSAKeyValue(key));
        signedXml.set_KeyInfo(keyInfo);
        // Compute the signature.
        signedXml.ComputeSignature();
        // Get the XML representation of the signature.
        XmlElement xmlSignature = signedXml.GetXml();
        Console.WriteLine(xmlSignature.get_OuterXml());
    } //main
} //XmlDsigSample1 

The following code example demonstrates how to check an XML signature.

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

public class Verify {

    public static void Main(String[] args) 
    {

        Console.WriteLine("Verifying " + args[0] + "...");

    	// Create a SignedXml.
        SignedXml signedXml = new SignedXml();

        // Load the XML.
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.PreserveWhitespace = true;
        xmlDocument.Load(new XmlTextReader(args[0]));

        XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
        signedXml.LoadXml((XmlElement)nodeList[0]);

        if (signedXml.CheckSignature()) {
            Console.WriteLine("Signature check OK");
        } else {
            Console.WriteLine("Signature check FAILED");
        }

    }
}

System.Object
  System.Security.Cryptography.Xml.DataObject

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft