Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
DataObject Class

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)

<HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort := True)> _
Public Class DataObject

The DataObject type exposes the following members.

Public methodDataObjectInitializes a new instance of the DataObject class.
Public methodDataObject(String, String, String, XmlElement)Initializes a new instance of the DataObject class with the specified identification, MIME type, encoding, and data.

Public propertyDataGets or sets the data value of the current DataObject object.
Public propertyEncodingGets or sets the encoding of the current DataObject object.
Public propertyIdGets or sets the identification of the current DataObject object.
Public propertyMimeTypeGets or sets the MIME type of the current DataObject object.

Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodGetXmlReturns the XML representation of the DataObject object.
Public methodLoadXmlLoads a DataObject state from an XML element.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

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 HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following code example demonstrates how to generate an enveloping XML signature.

Imports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Xml


Public Class XMLdsigsample1

   Shared Sub Main(args() As [String])
      ' Create example data to sign. 
      Dim document As New XmlDocument()
      Dim node As XmlNode = document.CreateNode(XmlNodeType.Element, "", "MyElement", "samples")
      node.InnerText = "This is some text"
      Console.Error.WriteLine("Data to sign:")

      ' Create the SignedXml message. 
      Dim signedXml As New SignedXml()
      Dim key As RSA = RSA.Create()
      signedXml.SigningKey = key

      ' Create a data object to hold the data to sign. 
      Dim dataObject As New DataObject()
      dataObject.Data = document.ChildNodes
      dataObject.Id = "MyObjectId" 

      ' Add the data object to the signature.

      ' Create a reference to be able to package everything into the 
      ' message. 
      Dim reference As New Reference()
      reference.Uri = "#MyObjectId" 

      ' Add it to the message.

      ' Add a KeyInfo. 
      Dim keyInfo As New KeyInfo()
      keyInfo.AddClause(New RSAKeyValue(key))
      signedXml.KeyInfo = keyInfo

      ' Compute the signature.

      ' Get the XML representation of the signature. 
      Dim xmlSignature As XmlElement = signedXml.GetXml()
   End Sub 'Main
End Class 'XMLdsigsample1 

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

Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.IO
Imports System.Xml


Public Class Verify

   Public Shared Sub Main(args() As [String])

      Console.WriteLine(("Verifying " + args(0) + "..."))

      ' Create a SignedXml. 
      Dim signedXml As New SignedXml()

      ' Load the XML. 
      Dim xmlDocument As New XmlDocument()
      xmlDocument.PreserveWhitespace = True
      xmlDocument.Load(New XmlTextReader(args(0)))

      Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature")
      signedXml.LoadXml(CType(nodeList(0), XmlElement))

      If signedXml.CheckSignature() Then
         Console.WriteLine("Signature check OK")
         Console.WriteLine("Signature check FAILED")
      End If 
   End Sub 'Main 
End Class 'Verify

.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

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