SignedXml.AddObject(DataObject) Метод
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Добавляет объект DataObject к списку подписываемых объектов.
public:
void AddObject(System::Security::Cryptography::Xml::DataObject ^ dataObject);
public void AddObject(System.Security.Cryptography.Xml.DataObject dataObject);
member this.AddObject : System.Security.Cryptography.Xml.DataObject -> unit
Public Sub AddObject (dataObject As DataObject)
Параметры
- dataObject
- DataObject
Объект DataObject, который добавляется к списку подписываемых объектов.
Примеры
В следующем примере кода выполняется вычисление и XML-сигнатура.
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)
{
try
{
// 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.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 the reference 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();
Console.WriteLine("The data was signed.");
}
catch(CryptographicException e)
{
Console.WriteLine(e.Message);
}
}
}
Imports System.IO
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Xml
Public Class XMLdsigsample1
Overloads Shared Sub Main(args() As [String])
Try
' 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.WriteLine(("Data to sign:" + ControlChars.Lf + document.OuterXml + ControlChars.Lf))
' 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 the reference 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()
Console.WriteLine("The data was signed.")
Catch e As CryptographicException
Console.WriteLine(e.Message)
End Try
End Sub
End Class
Комментарии
Метод AddObject добавляет <Object>
элемент , представляющий объект для подписи, в <Signature>
элемент цифровой подписи XML.
Метод AddObject внутренне вызывает AddObject метод объекта, Signature инкапсулированного SignedXml объектом . Вы также можете добавить объект, DataObject напрямую вызвав AddObject метод из Signature свойства .
Дополнительные сведения о цифровых подписях XML см. в спецификации XMLDSIG.