XmlDsigExcC14NWithCommentsTransform Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Vertegenwoordigt de exclusieve C14N XML-canonicalisatietransformatie voor een digitale handtekening zoals gedefinieerd door het World Wide Web Consortium (W3C), met opmerkingen.
public ref class XmlDsigExcC14NWithCommentsTransform : System::Security::Cryptography::Xml::XmlDsigExcC14NTransform
public class XmlDsigExcC14NWithCommentsTransform : System.Security.Cryptography.Xml.XmlDsigExcC14NTransform
type XmlDsigExcC14NWithCommentsTransform = class
inherit XmlDsigExcC14NTransform
Public Class XmlDsigExcC14NWithCommentsTransform
Inherits XmlDsigExcC14NTransform
- Overname
Voorbeelden
In het volgende codevoorbeeld ziet u hoe u een XML-document ondertekent en verifieert met behulp van de XmlDsigExcC14NWithCommentsTransform klasse. In dit voorbeeld wordt een envelophandtekening gemaakt.
//
// This example signs an XML file using an
// envelope signature. It then verifies the
// signed XML.
//
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
public class SignVerifyEnvelope
{
public static void Main(String[] args)
{
try
{
// Generate a signing key.
RSA Key = RSA.Create();
// Create an XML file to sign.
CreateSomeXml("Example.xml");
Console.WriteLine("New XML file created.");
// Sign the XML that was just created and save it in a
// new file.
//SignXmlFile("Example.xml", "SignedExample.xml", Key);
Console.WriteLine("XML file signed.");
// Verify the signature of the signed XML.
Console.WriteLine("Verifying signature...");
bool result = VerifyXmlFile("SignedExample.xml");
// Display the results of the signature verification to
// the console.
if (result)
{
Console.WriteLine("The XML signature is valid.");
}
else
{
Console.WriteLine("The XML signature is not valid.");
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}
// Sign an XML file and save the signature in a new file.
public static void SignXmlFile(string FileName, string SignedFileName, RSA Key)
{
// Create a new XML document.
XmlDocument doc = new XmlDocument();
// Format the document to ignore white spaces.
doc.PreserveWhitespace = false;
// Load the passed XML file using it's name.
doc.Load(new XmlTextReader(FileName));
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(doc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Specify a canonicalization method.
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NWithCommentsTransformUrl;
// Set the InclusiveNamespacesPrefixList property.
XmlDsigExcC14NWithCommentsTransform canMethod = (XmlDsigExcC14NWithCommentsTransform)signedXml.SignedInfo.CanonicalizationMethodObject;
canMethod.InclusiveNamespacesPrefixList = "Sign";
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new RSAKeyValue((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));
if (doc.FirstChild is XmlDeclaration)
{
doc.RemoveChild(doc.FirstChild);
}
// Save the signed XML document to a file specified
// using the passed string.
XmlTextWriter xmltw = new XmlTextWriter(SignedFileName, new UTF8Encoding(false));
doc.WriteTo(xmltw);
xmltw.Close();
}
// Verify the signature of an XML file and return the result.
public static Boolean VerifyXmlFile(String Name)
{
// Create a new XML document.
XmlDocument xmlDocument = new 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 = new SignedXml(xmlDocument);
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Signature");
// Load the signature node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature();
}
// Create example data to sign.
public static void CreateSomeXml(string FileName)
{
// Create a new XmlDocument object.
XmlDocument document = new XmlDocument();
// Create a new XmlNode object.
XmlNode node = document.CreateNode(XmlNodeType.Element, "", "MyXML", "Don't_Sign");
// Append the node to the document.
document.AppendChild(node);
// Create a new XmlNode object.
XmlNode subnode = document.CreateNode(XmlNodeType.Element, "", "TempElement", "Sign");
// Add some text to the node.
subnode.InnerText = "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 = new XmlTextWriter(FileName, new UTF8Encoding(false));
document.WriteTo(xmltw);
xmltw.Close();
}
}
'
' This example signs an XML file using an
' envelope signature. It then verifies the
' signed XML.
'
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Cryptography.Xml
Imports System.Text
Imports System.Xml
Module SignVerifyEnvelope
Sub Main(ByVal args() As String)
Try
' Generate a signing key.
Dim Key As RSA = RSA.Create()
' Create an XML file to sign.
CreateSomeXml("Example.xml")
Console.WriteLine("New XML file created.")
' Sign the XML that was just created and save it in a
' new file.
'SignXmlFile("Example.xml", "SignedExample.xml", Key);
Console.WriteLine("XML file signed.")
' Verify the signature of the signed XML.
Console.WriteLine("Verifying signature...")
Dim result As Boolean = VerifyXmlFile("SignedExample.xml")
' Display the results of the signature verification to
' the console.
If result Then
Console.WriteLine("The XML signature is valid.")
Else
Console.WriteLine("The XML signature is not valid.")
End If
Catch e As CryptographicException
Console.WriteLine(e.Message)
End Try
End Sub
' Sign an XML file and save the signature in a new file.
Sub SignXmlFile(ByVal FileName As String, ByVal SignedFileName As String, ByVal Key As RSA)
' Create a new XML document.
Dim doc As New XmlDocument()
' Format the document to ignore white spaces.
doc.PreserveWhitespace = False
' Load the passed XML file using it's name.
doc.Load(New XmlTextReader(FileName))
' Create a SignedXml object.
Dim signedXml As New SignedXml(doc)
' Add the key to the SignedXml document.
signedXml.SigningKey = Key
' Specify a canonicalization method.
signedXml.SignedInfo.CanonicalizationMethod = signedXml.XmlDsigExcC14NWithCommentsTransformUrl
' Set the InclusiveNamespacesPrefixList property.
Dim canMethod As XmlDsigExcC14NWithCommentsTransform = CType(signedXml.SignedInfo.CanonicalizationMethodObject, XmlDsigExcC14NWithCommentsTransform)
canMethod.InclusiveNamespacesPrefixList = "Sign"
' Create a reference to be signed.
Dim reference As New Reference()
reference.Uri = ""
' Add an enveloped transformation to the reference.
Dim env As New XmlDsigEnvelopedSignatureTransform()
reference.AddTransform(env)
' Add the reference to the SignedXml object.
signedXml.AddReference(reference)
' Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
Dim keyInfo As New KeyInfo()
keyInfo.AddClause(New RSAKeyValue(CType(Key, RSA)))
signedXml.KeyInfo = keyInfo
' Compute the signature.
signedXml.ComputeSignature()
' Get the XML representation of the signature and save
' it to an XmlElement object.
Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
' Append the element to the XML document.
doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, True))
If TypeOf doc.FirstChild Is XmlDeclaration Then
doc.RemoveChild(doc.FirstChild)
End If
' Save the signed XML document to a file specified
' using the passed string.
Dim xmltw As New XmlTextWriter(SignedFileName, New UTF8Encoding(False))
doc.WriteTo(xmltw)
xmltw.Close()
End Sub
' Verify the signature of an XML file and return the result.
Function VerifyXmlFile(ByVal Name As String) As [Boolean]
' Create a new XML document.
Dim xmlDocument As New 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.
Dim signedXml As New SignedXml(xmlDocument)
' Find the "Signature" node and create a new
' XmlNodeList object.
Dim nodeList As XmlNodeList = xmlDocument.GetElementsByTagName("Signature")
' Load the signature node.
signedXml.LoadXml(CType(nodeList(0), XmlElement))
' Check the signature and return the result.
Return signedXml.CheckSignature()
End Function
' Create example data to sign.
Sub CreateSomeXml(ByVal FileName As String)
' Create a new XmlDocument object.
Dim document As New XmlDocument()
' Create a new XmlNode object.
Dim node As XmlNode = document.CreateNode(XmlNodeType.Element, "", "MyXML", "Don't_Sign")
' Append the node to the document.
document.AppendChild(node)
' Create a new XmlNode object.
Dim subnode As XmlNode = document.CreateNode(XmlNodeType.Element, "", "TempElement", "Sign")
' Add some text to the node.
subnode.InnerText = "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.
Dim xmltw As New XmlTextWriter(FileName, New UTF8Encoding(False))
document.WriteTo(xmltw)
xmltw.Close()
End Sub
End Module
Opmerkingen
De XmlDsigExcC14NWithCommentsTransform klasse vertegenwoordigt de exclusieve C14N XML-canonicalisatietransformatie met opmerkingen. Deze klasse is vergelijkbaar met de XmlDsigExcC14NTransform klasse, waardoor een ondertekenaar een samenvatting kan maken met behulp van de canonieke vorm van een XML-document. De XmlDsigExcC14NWithCommentsTransform klasse sluit echter bovenliggende context uit van een canonieke subdocument.
Gebruik de XmlDsigExcC14NWithCommentsTransform klasse wanneer u een XML-subdocument moet canonicaliseren zodat het onafhankelijk is van de XML-context. Toepassingen zoals webservices die gebruikmaken van ondertekende XML binnen complexe communicatieprotocollen, moeten XML bijvoorbeeld vaak op deze manier canonicaliseren. Dergelijke toepassingen bevatten vaak XML binnen verschillende dynamisch samengestelde elementen, waardoor het document aanzienlijk kan worden gewijzigd en dat verificatie van XML-handtekeningen mislukt. De XmlDsigExcC14NWithCommentsTransform klasse lost dit probleem op door dergelijke bovenliggende context uit te sluiten van het canonieke subdocument.
U kunt geen nieuw exemplaar van een canonicalisatietransformatieklasse maken. Als u een canonicalisatietransformatie wilt opgeven, geeft u de URI (Uniform Resource Identifier) door die de transformatie beschrijft naar de CanonicalizationMethod eigenschap, die toegankelijk is vanuit de SignedInfo eigenschap. Als u een verwijzing naar de canonicalisatietransformatie wilt verkrijgen, gebruikt u de CanonicalizationMethodObject eigenschap die toegankelijk is vanuit de SignedInfo eigenschap.
De URI die de XmlDsigExcC14NWithCommentsTransform klasse beschrijft, wordt gedefinieerd door het XmlDsigExcC14NWithCommentsTransformUrl veld.
Zie de XMLDSIG-specificatie, die beschikbaar is op de W3C-website voor meer informatie over de exclusieve C14N-transformatie. Het canonicalisatie-algoritme wordt gedefinieerd in de W3C Canonical XML-specificatie.
Constructors
| Name | Description |
|---|---|
| XmlDsigExcC14NWithCommentsTransform() |
Initialiseert een nieuw exemplaar van de XmlDsigExcC14NWithCommentsTransform klasse. |
| XmlDsigExcC14NWithCommentsTransform(String) |
Initialiseert een nieuw exemplaar van de XmlDsigExcC14NWithCommentsTransform klasse waarin een lijst met naamruimtevoorvoegsels wordt opgegeven om canonicaliseren te maken met behulp van het standaardgoritme voor canonicalisatie. |
Eigenschappen
| Name | Description |
|---|---|
| Algorithm |
Haalt de URI (Uniform Resource Identifier) op of stelt deze in die het algoritme identificeert dat wordt uitgevoerd door de huidige transformatie. (Overgenomen van Transform) |
| Context |
Hiermee wordt een XmlElement object opgehaald of ingesteld dat de documentcontext vertegenwoordigt waaronder het huidige Transform object wordt uitgevoerd. (Overgenomen van Transform) |
| InclusiveNamespacesPrefixList |
Hiermee haalt u een tekenreeks op die naamruimtevoorvoegsels bevat om canonicaliseren te maken met behulp van het standaard-canonicalisatie-algoritme. (Overgenomen van XmlDsigExcC14NTransform) |
| InputTypes |
Hiermee haalt u een matrix op van typen die geldige invoer zijn voor de LoadInput(Object) methode van het huidige XmlDsigExcC14NTransform object. (Overgenomen van XmlDsigExcC14NTransform) |
| OutputTypes |
Hiermee haalt u een matrix op van typen die mogelijke uitvoer van de GetOutput() methoden van het huidige XmlDsigExcC14NTransform object zijn. (Overgenomen van XmlDsigExcC14NTransform) |
| PropagatedNamespaces |
Hiermee wordt een Hashtable object opgehaald of ingesteld dat de naamruimten bevat die in de handtekening worden doorgegeven. (Overgenomen van Transform) |
| Resolver |
Hiermee stelt u het huidige XmlResolver object in. (Overgenomen van Transform) |
Methoden
| Name | Description |
|---|---|
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetDigestedOutput(HashAlgorithm) |
Retourneert de samenvatting die is gekoppeld aan een XmlDsigExcC14NTransform object. (Overgenomen van XmlDsigExcC14NTransform) |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetInnerXml() |
Retourneert een XML-weergave van de parameters van een XmlDsigExcC14NTransform object dat geschikt is om te worden opgenomen als subelementen van een XMLDSIG-element |
| GetOutput() |
Retourneert de uitvoer van het huidige XmlDsigExcC14NTransform object. (Overgenomen van XmlDsigExcC14NTransform) |
| GetOutput(Type) |
Retourneert de uitvoer van het huidige XmlDsigExcC14NTransform object als een object van het opgegeven type. (Overgenomen van XmlDsigExcC14NTransform) |
| GetType() |
Hiermee haalt u de Type huidige instantie op. (Overgenomen van Object) |
| GetXml() |
Retourneert de XML-weergave van het huidige Transform object. (Overgenomen van Transform) |
| LoadInnerXml(XmlNodeList) |
Parseert het opgegeven XmlNodeList object als transformatiespecifieke inhoud van een |
| LoadInput(Object) |
Wanneer deze wordt overschreven in een afgeleide klasse, wordt de opgegeven invoer in het huidige XmlDsigExcC14NTransform object geladen. (Overgenomen van XmlDsigExcC14NTransform) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |