次の方法で共有


SignedXml.AddReference メソッド

ダイジェストおよび署名される参照のリストに Reference を追加します。

Public Sub AddReference( _
   ByVal reference As Reference _)
[C#]
public void AddReference(Referencereference);
[C++]
public: void AddReference(Reference* reference);
[JScript]
public function AddReference(
   reference : Reference);

パラメータ

  • reference
    ダイジェストおよび署名される参照のリストに追加する Reference

使用例

 
Imports System
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 

[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)
    {
        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);
        }
    }
}

[C++] 
// This example signs an XML file using an
// envelope signature. It then verifies the 
// signed XML.

#using <mscorlib.dll>
#using <mscorlib.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::Text;
using namespace System::Xml;


// Sign an XML file and save the signature in a new file.

void SignXmlFile(String* FileName, String* SignedFileName, RSA* RSAKey) {
   // 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 its name.
   doc->Load(new XmlTextReader(FileName));

   // Create a SignedXml object.
   SignedXml* signedXml = new SignedXml(doc);

   // Add the RSA key to the SignedXml document. 
   signedXml->SigningKey = RSAKey;

   // Create a reference to be signed.
   Reference* reference = new Reference();
   reference->Uri = S"";

   // Add a transformation to the reference.
   Transform* trns = new XmlDsigC14NTransform();
   reference->AddTransform(trns);

   // 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 to the KeyInfo (optional; helps recipient find key to validate).
   KeyInfo* keyInfo = new KeyInfo();
   keyInfo->AddClause(new RSAKeyValue(__try_cast<RSA*>(RSAKey))); 
   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)->GetType() == __typeof(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.

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(S"Signature");

   // Load the signature node.
   signedXml->LoadXml(__try_cast<XmlElement*>( nodeList->Item(0) ));

   // Check the signature and return the result.
   return signedXml->CheckSignature();
}

// Create example data to sign.

void CreateSomeXml(String* FileName) {
   // Create a new XmlDocument object.
   XmlDocument* document = new XmlDocument();

   // Create a new XmlNode object.
   XmlNode*  node = document->CreateNode(XmlNodeType::Element, S"", S"MyElement", S"samples");

   // Add some text to the node.
   node->InnerText = S"Example text to be signed.";

   // Append the node to the document.
   document->AppendChild(node);

   // Save the XML document to the file name specified.
   XmlTextWriter* xmltw = new XmlTextWriter(FileName, new UTF8Encoding(false));
   document->WriteTo(xmltw);
   xmltw->Close();
}

int main() {
   try {
      // Generate an RSA signing key.
      RSACryptoServiceProvider* RSAKey = new RSACryptoServiceProvider();

      // Create an XML file to sign.
      CreateSomeXml(S"Example.xml");
      Console::WriteLine(S"New XML file created."); 

      // Sign the XML that was just created and save it in a 
      // new file.
      SignXmlFile(S"Example.xml", S"SignedExample.xml", RSAKey);
      Console::WriteLine(S"XML file signed."); 

      // Verify the signature of the signed XML.
      Console::WriteLine(S"Verifying signature...");
      bool result = VerifyXmlFile(S"SignedExample.xml");

      // Display the results of the signature verification to
      // the console.
      if (result) {
         Console::WriteLine(S"The XML signature is valid.");
      } else {
         Console::WriteLine(S"The XML signature is not valid.");
      }
   } catch (CryptographicException* e) {
      Console::WriteLine(e->Message);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

SignedXml クラス | SignedXml メンバ | System.Security.Cryptography.Xml 名前空間