XmlDsigXPathTransform Class
Represents the XPath transform for a digital signature as defined by the W3C.
System.Security.Cryptography.Xml::Transform
System.Security.Cryptography.Xml::XmlDsigXPathTransform
Assembly: System.Security (in System.Security.dll)
The XmlDsigXPathTransform type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | XmlDsigXPathTransform | Initializes a new instance of the XmlDsigXPathTransform class. |
| Name | Description | |
|---|---|---|
![]() | Algorithm | Gets or sets the Uniform Resource Identifier (URI) that identifies the algorithm performed by the current transform. (Inherited from Transform.) |
![]() | Context | Gets or sets an XmlElement object that represents the document context under which the current Transform object is running. (Inherited from Transform.) |
![]() | InputTypes | Gets an array of types that are valid inputs to the LoadInput method of the current XmlDsigXPathTransform object. (Overrides Transform::InputTypes.) |
![]() | OutputTypes | Gets an array of types that are possible outputs from the GetOutput methods of the current XmlDsigXPathTransform object. (Overrides Transform::OutputTypes.) |
![]() | PropagatedNamespaces | Gets or sets a Hashtable object that contains the namespaces that are propagated into the signature. (Inherited from Transform.) |
![]() | Resolver | Sets the current XmlResolver object. (Inherited from Transform.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetDigestedOutput | When overridden in a derived class, returns the digest associated with a Transform object. (Inherited from Transform.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetInnerXml | Returns an XML representation of the parameters of a XmlDsigXPathTransform object that are suitable to be included as subelements of an XMLDSIG <Transform> element. (Overrides Transform::GetInnerXml().) |
![]() | GetOutput() | Returns the output of the current XmlDsigXPathTransform object. (Overrides Transform::GetOutput().) |
![]() | GetOutput(Type) | Returns the output of the current XmlDsigXPathTransform object of type XmlNodeList. (Overrides Transform::GetOutput(Type).) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetXml | Returns the XML representation of the current Transform object. (Inherited from Transform.) |
![]() | LoadInnerXml | Parses the specified XmlNodeList object as transform-specific content of a <Transform> element and configures the internal state of the current XmlDsigXPathTransform object to match the <Transform> element. (Overrides Transform::LoadInnerXml(XmlNodeList).) |
![]() | LoadInput | Loads the specified input into the current XmlDsigXPathTransform object. (Overrides Transform::LoadInput(Object).) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
The XmlDsigXPathTransform class allows you to address an XML document using the XPath language. This class uses an XPath expression to select the XML nodes you want to sign or encrypt while removing all others from either cryptographic operation.
Use the XmlDsigXPathTransform class to sign or encrypt specific parts of an XML document using an XPath expression. For information on initializing the XmlDsigXPathTransform class to the value of an XPath transform string, see the LoadInnerXml method.
The Uniform Resource Identifier (URI) that describes the XmlDsigXPathTransform class is defined by the XmlDsigXPathTransformUrl field.
For more information about the XPath transform, see Section 6.6.3 of the XMLDSIG specification, which is available from the W3C at www.w3.org/TR/xmldsig-core/.
Note |
|---|
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. |
This section contains two code examples. The first code example shows how to sign and verify an XML document using the XmlDsigXPathTransform class with an envelope signature. This example signs an XML document and saves the signature in a new XML document. The second code example demonstrates how to call members of the XmlDsigXPathTransform class.
Example #1
// // This example signs an XML file using an // envelope signature. It then verifies the // signed XML. // #using <System.Xml.dll> #using <System.Security.dll> #using <System.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::X509Certificates; using namespace System::Security::Cryptography::Xml; using namespace System::Text; using namespace System::Xml; // Create the XML that represents the transform. static XmlDsigXPathTransform^ CreateXPathTransform( String^ XPathString ) { // Create a new XMLDocument object. XmlDocument^ doc = gcnew XmlDocument; // Create a new XmlElement. XmlElement^ xPathElem = doc->CreateElement( L"XPath" ); // Set the element text to the value // of the XPath string. xPathElem->InnerText = XPathString; // Create a new XmlDsigXPathTransform object. XmlDsigXPathTransform^ xForm = gcnew XmlDsigXPathTransform; // Load the XPath XML from the element. xForm->LoadInnerXml( xPathElem->SelectNodes( L"." ) ); // Return the XML that represents the transform. return xForm; } // Sign an XML file and save the signature in a new file. static void SignXmlFile( String^ FileName, String^ SignedFileName, RSA^ Key, String^ XPathString ) { // Create a new XML document. XmlDocument^ doc = gcnew XmlDocument; // Format the document to ignore white spaces. doc->PreserveWhitespace = false; // Load the passed XML file using it's name. doc->Load( gcnew XmlTextReader( FileName ) ); // Create a SignedXml object. SignedXml^ signedXml = gcnew SignedXml( doc ); // Add the key to the SignedXml document. signedXml->SigningKey = Key; // Create a reference to be signed. Reference^ reference = gcnew Reference; reference->Uri = L""; // Create an XmlDsigXPathTransform object using // the helper method 'CreateXPathTransform' defined // later in this sample. XmlDsigXPathTransform^ XPathTransform = CreateXPathTransform( XPathString ); // Add the transform to the reference. reference->AddTransform( XPathTransform ); // Add the reference to the SignedXml object. signedXml->AddReference( reference ); // Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate). KeyInfo^ keyInfo = gcnew KeyInfo; keyInfo->AddClause( gcnew RSAKeyValue( dynamic_cast<RSA^>(Key) ) ); signedXml->KeyInfo = keyInfo; // Compute the signature. signedXml->ComputeSignature(); // Get the XML representation of the signature and save // it to an XmlElement object. XmlElement^ xmlDigitalSignature = signedXml->GetXml(); // Append the element to the XML document. doc->DocumentElement->AppendChild( doc->ImportNode( xmlDigitalSignature, true ) ); // Save the signed XML document to a file specified // using the passed string. XmlTextWriter^ xmltw = gcnew XmlTextWriter( SignedFileName,gcnew UTF8Encoding( false ) ); doc->WriteTo( xmltw ); xmltw->Close(); } // Verify the signature of an XML file and return the result. static Boolean VerifyXmlFile( String^ Name ) { // Create a new XML document. XmlDocument^ xmlDocument = gcnew XmlDocument; // Format using white spaces. xmlDocument->PreserveWhitespace = true; // Load the passed XML file into the document. xmlDocument->Load( Name ); // Create a new SignedXml object and pass it // the XML document class. SignedXml^ signedXml = gcnew SignedXml( xmlDocument ); // Find the "Signature" node and create a new // XmlNodeList object. XmlNodeList^ nodeList = xmlDocument->GetElementsByTagName( L"Signature" ); // Load the signature node. signedXml->LoadXml( dynamic_cast<XmlElement^>(nodeList->Item( 0 )) ); // Check the signature and return the result. return signedXml->CheckSignature(); } // Create example data to sign. static void CreateSomeXml( String^ FileName ) { // Create a new XmlDocument object. XmlDocument^ document = gcnew XmlDocument; // Create a new XmlNode object. XmlNode^ node = document->CreateNode( XmlNodeType::Element, L"", L"MyXML", L"Don't_Sign" ); // Append the node to the document. document->AppendChild( node ); // Create a new XmlNode object. XmlNode^ subnode = document->CreateNode( XmlNodeType::Element, L"", L"TempElement", L"Sign" ); // Add some text to the node. subnode->InnerText = L"Here is some data to sign."; // Append the node to the document. document->DocumentElement->AppendChild( subnode ); // Save the XML document to the file name specified. XmlTextWriter^ xmltw = gcnew XmlTextWriter( FileName,gcnew UTF8Encoding( false ) ); document->WriteTo( xmltw ); xmltw->Close(); } int main() { // Generate a signing key. RSACryptoServiceProvider^ Key = gcnew RSACryptoServiceProvider; try { // Create an XML file to sign. CreateSomeXml( L"Example.xml" ); Console::WriteLine( L"New XML file created." ); // Sign the XML that was just created and save it in a // new file. SignXmlFile( L"Example.xml", L"SignedExample.xml", Key, L"ancestor-or-self::TempElement" ); Console::WriteLine( L"XML file signed." ); // Verify the signature of the signed XML. Console::WriteLine( L"Verifying signature..." ); bool result = VerifyXmlFile( L"SignedExample.xml" ); // Display the results of the signature verification to \ // the console. if ( result ) { Console::WriteLine( L"The XML signature is valid." ); } else { Console::WriteLine( L"The XML signature is not valid." ); } } catch ( CryptographicException^ e ) { Console::WriteLine( e->Message ); } finally { Key->Clear(); } return 1; }
Example #2
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
