DataObject Class
Represents the object element of an XML signature that holds the data to be signed.
For a list of all members of this type, see DataObject Members.
System.Object
System.Security.Cryptography.Xml.DataObject
[Visual Basic] Public Class DataObject [C#] public class DataObject [C++] public __gc class DataObject [JScript] public class DataObject
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Example
[Visual Basic, C#, C++] The following example demonstrates how to generate an enveloping XML signature.
[Visual Basic] 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" document.AppendChild(node) Console.Error.WriteLine("Data to sign:") Console.Error.WriteLine() Console.Error.WriteLine(document.OuterXml) Console.Error.WriteLine() ' 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. signedXml.AddObject(dataObject) ' 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. signedXml.AddReference(reference) ' Add a KeyInfo. Dim keyInfo As New KeyInfo() keyInfo.AddClause(New RSAKeyValue(key)) signedXml.KeyInfo = keyInfo ' Compute the signature. signedXml.ComputeSignature() ' Get the XML representation of the signature. Dim xmlSignature As XmlElement = signedXml.GetXml() Console.WriteLine(xmlSignature.OuterXml) End Sub 'Main End Class 'XMLdsigsample1 [C#] 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); } } [C++] #using <mscorlib.dll> #using <System.dll> #using <System.Xml.dll> #using <System.Security.dll> using namespace System; using namespace System::IO; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::Xml; int main() { // Create example data to sign. XmlDocument* document = new XmlDocument(); XmlNode* node = document->CreateNode(XmlNodeType::Element, S"", S"MyElement", S"samples"); node->InnerText = S"This is some text"; document->AppendChild(node); Console::Error->WriteLine( S"Data to sign:\n{0}\n", document->OuterXml ); // 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 = S"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 = S"#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); }
[Visual Basic, C#, C++] The following example demonstrates how to check an XML signature.
[Visual Basic] 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") Else Console.WriteLine("Signature check FAILED") End If End Sub 'Main End Class 'Verify [C#] 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"); } } } [C++] #using <mscorlib.dll> #using <System.dll> #using <System.Security.dll> #using <System.Xml.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::Xml; using namespace System::IO; using namespace System::Xml; int main() { String* args[] = System::Environment::GetCommandLineArgs(); Console::WriteLine(S"Verifying {0}...", args[1]); // Create a SignedXml. SignedXml* signedXml = new SignedXml(); // Load the XML. XmlDocument* xmlDocument = new XmlDocument(); xmlDocument->PreserveWhitespace = true; xmlDocument->Load(new XmlTextReader(args[1])); XmlNodeList* nodeList = xmlDocument->GetElementsByTagName(S"Signature"); signedXml->LoadXml(__try_cast<XmlElement*>(nodeList->ItemOf[0])); if (signedXml->CheckSignature()) { Console::WriteLine(S"Signature check OK"); } else { Console::WriteLine(S"Signature check FAILED"); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Security.Cryptography.Xml
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System.Security (in System.Security.dll)
See Also
DataObject Members | System.Security.Cryptography.Xml Namespace