XmlDsigBase64Transform Class

Definition

Represents the Base64 decoding transform as defined in Section 6.6.2 of the XMLDSIG specification.

C#
public class XmlDsigBase64Transform : System.Security.Cryptography.Xml.Transform
Inheritance
XmlDsigBase64Transform

Examples

The following code example demonstrates how to use members of the XmlDsigBase64Transform class.

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

class Class1
{
    [STAThread]
    static void Main(string[] args)
    {
        // Encrypt an XML message
        EncryptXML(LoadXMLDoc());

        // Using XmlDsigBase64Transform resolving a Uri.
        Uri baseUri = new Uri("http://www.microsoft.com");
        string relativeUri = "msdn";
        Uri absoluteUri = ResolveUris(baseUri, relativeUri);

        Console.WriteLine("This sample completed successfully; " +
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Encrypt the text in the specified XmlDocument.
    private static void EncryptXML(XmlDocument xmlDoc)
    {
        XmlDsigBase64Transform xmlTransform = new XmlDsigBase64Transform();

        // Ensure the transform is using the proper algorithm.
        xmlTransform.Algorithm = SignedXml.XmlDsigBase64TransformUrl;

        // Retrieve the XML representation of the current transform.
        XmlElement xmlInTransform = xmlTransform.GetXml();

        Console.WriteLine("Xml representation of the current transform: ");
        Console.WriteLine(xmlInTransform.OuterXml);

        // Retrieve the valid input types for the current transform.
        Type[] validInTypes = xmlTransform.InputTypes;

        // Verify the xmlTransform can accept the XMLDocument as an
        // input type.
        for (int i=0; i<validInTypes.Length; i++)
        {
            if (validInTypes[i] == xmlDoc.GetType())
            {
                // Demonstrate loading the entire Xml Document.
                xmlTransform.LoadInput(xmlDoc);

                // This transform is created for demonstration purposes.
                XmlDsigBase64Transform secondTransform =
                    new XmlDsigBase64Transform();

                string classDescription = secondTransform.ToString();

                // This call does not perform as expected.
                // LoadInnerXml is overridden by the XmlDsigBase64Transform
                // class, but is stubbed out.
                secondTransform.LoadInnerXml(xmlDoc.SelectNodes("//."));

                break;
            }
        }

        Type[] validOutTypes = xmlTransform.OutputTypes;

        for (int i=0; i<validOutTypes.Length; i++)
        {
            if (validOutTypes[i] == typeof(System.IO.Stream))
            {
                try 
                {
                    Type streamType = typeof(System.IO.Stream);
                    CryptoStream outputStream = (CryptoStream) 
                        xmlTransform.GetOutput(streamType);

                    // Read the CryptoStream into a stream reader.
                    StreamReader streamReader =
                        new StreamReader(outputStream);

                    // Read the stream into a string.
                    string outputMessage = streamReader.ReadToEnd();

                    // Close the streams.
                    outputStream.Close();
                    streamReader.Close();

                    // Display to the console the Xml before and after
                    // encryption.
                    Console.WriteLine("Encoding the following message: " +
                        xmlDoc.InnerText);
                    Console.WriteLine("Message encoded: " + outputMessage);
            }
                catch (Exception ex)
                {
                    Console.WriteLine("Unexpected exception caught: " +
                        ex.ToString());
                }

                break;
            }
            else
            {
                object outputObject = xmlTransform.GetOutput();
            }
        }
    }

    // Create an XML document with Element and Text nodes.
    private static XmlDocument LoadXMLDoc()
    {
        XmlDocument xmlDoc = new XmlDocument();

        XmlNode mainNode = xmlDoc.CreateNode(
            XmlNodeType.Element,
            "ContosoMessages",
            "http://www.contoso.com");

        XmlNode textNode = xmlDoc.CreateTextNode("Some text to encode.");
        mainNode.AppendChild(textNode);
        xmlDoc.AppendChild(mainNode);

        Console.WriteLine("Created the following XML Document for " +
            "transformation: ");
        Console.WriteLine(xmlDoc.InnerXml);
        return xmlDoc;
    }

    // Resolve the specified base and relative Uri's .
    private static Uri ResolveUris(Uri baseUri, string relativeUri)
    {
        XmlUrlResolver xmlResolver = new XmlUrlResolver();
        xmlResolver.Credentials = 
            System.Net.CredentialCache.DefaultCredentials;

        XmlDsigBase64Transform xmlTransform = new XmlDsigBase64Transform();
        xmlTransform.Resolver = xmlResolver;

        Uri absoluteUri = xmlResolver.ResolveUri(baseUri, relativeUri);

        if (absoluteUri != null)
        {
            Console.WriteLine(
                "Resolved the base Uri and relative Uri to the following:");
            Console.WriteLine(absoluteUri.ToString());
        }
        else
        {
            Console.WriteLine(
                "Unable to resolve the base Uri and relative Uri");
        }
        return absoluteUri;
    }
}
//
// This sample produces the following output:
//
// Created the following XML Document for transformation:
// <ContosoMessages xmlns="http://www.contoso.com">Some text to encode.
// Xml representation of the current transform:
// <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#base64" xmlns=
// "http://www.w3.org/2000/09/xmldsig#" />
// Encoding the following message: Some text to encode.
// Message encoded: Jmr^
// Resolved the base Uri and relative Uri to the following:
// http://www.microsoft.com/msdn
// This sample completed successfully; press Enter to exit.

Remarks

Use the XmlDsigBase64Transform object when you need to sign the raw data associated with the encoded content of an element.

The Uniform Resource Identifier (URI) that describes the XmlDsigBase64Transform object is defined by the XmlDsigBase64TransformUrl field.

For more information about the Base64 decoding transform, see Section 6.6.2 of the W3C XMLDSIG specification.

Constructors

XmlDsigBase64Transform()

Initializes a new instance of the XmlDsigBase64Transform class.

Properties

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(Object) method of the current XmlDsigBase64Transform object.

OutputTypes

Gets an array of types that are possible outputs from the GetOutput() methods of the current XmlDsigBase64Transform object.

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)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDigestedOutput(HashAlgorithm)

When overridden in a derived class, returns the digest associated with a Transform object.

(Inherited from Transform)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetInnerXml()

Returns an XML representation of the parameters of the XmlDsigBase64Transform object that are suitable to be included as subelements of an XMLDSIG <Transform> element.

GetOutput()

Returns the output of the current XmlDsigBase64Transform object.

GetOutput(Type)

Returns the output of the current XmlDsigBase64Transform object of type Stream.

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(XmlNodeList)

Parses the specified XmlNodeList object as transform-specific content of a <Transform> element; this method is not supported because the XmlDsigBase64Transform object has no inner XML elements.

LoadInput(Object)

Loads the specified input into the current XmlDsigBase64Transform 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)

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9