SignedXml.AddObject メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SignedXml.AddObject メソッドの意味・解説 

SignedXml.AddObject メソッド

署名されるオブジェクトリストDataObject オブジェクト追加します

名前空間: System.Security.Cryptography.Xml
アセンブリ: System.Security (system.security.dll 内)
構文構文

Public Sub AddObject ( _
    dataObject As DataObject _
)
Dim instance As SignedXml
Dim dataObject As DataObject

instance.AddObject(dataObject)
public void AddObject (
    DataObject dataObject
)
public:
void AddObject (
    DataObject^ dataObject
)
public void AddObject (
    DataObject dataObject
)
public function AddObject (
    dataObject : DataObject
)

パラメータ

dataObject

署名されるオブジェクトリスト追加する DataObject オブジェクト

解説解説
使用例使用例

XML 署名計算するコード例次に示します

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 
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);
        }
    }
}
// This example signs an XML file using an
// envelope signature. It then verifies the 
// signed XML.
#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 = gcnew XmlDocument;
   
   // Format the document to ignore white spaces.
   doc->PreserveWhitespace = false;
   
   // Load the passed XML file using its name.
   doc->Load( gcnew XmlTextReader( FileName ) );
   
   // Create a SignedXml object.
   SignedXml^ signedXml = gcnew SignedXml( doc );
   
   // Add the RSA key to the SignedXml document. 
   signedXml->SigningKey = RSAKey;
   
   // Create a reference to be signed.
   Reference^ reference = gcnew Reference;
   reference->Uri = "";
   
   // Add a transformation to the reference.
   Transform^ trns = gcnew XmlDsigC14NTransform;
   reference->AddTransform( trns );
   
   // Add an enveloped transformation to the reference.
   XmlDsigEnvelopedSignatureTransform^ env = gcnew 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 = gcnew KeyInfo;
   keyInfo->AddClause( gcnew RSAKeyValue( safe_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() == XmlDeclaration::typeid
 )
   {
      doc->RemoveChild( doc->FirstChild );
   }

   
   // 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.
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( "Signature"
 );
   
   // Load the signature node.
   signedXml->LoadXml( safe_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 = gcnew XmlDocument;
   
   // Create a new XmlNode object.
   XmlNode^ node = document->CreateNode( XmlNodeType::Element, "", "MyElement",
 "samples" );
   
   // Add some text to the node.
   node->InnerText = "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 = gcnew XmlTextWriter( FileName,gcnew UTF8Encoding( false
 ) );
   document->WriteTo( xmltw );
   xmltw->Close();
}

int main()
{
   try
   {
      
      // Generate an RSA signing key.
      RSACryptoServiceProvider^ RSAKey = gcnew RSACryptoServiceProvider;
      
      // 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", RSAKey
 );
      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 );
   }

}

import System.*;
import System.IO.*;
import System.Security.Cryptography.*;
import System.Security.Cryptography.Xml.*;
import System.Xml.*;

public class XMLdsigsample1
{
    public static void main(String[]
 args)
    {
        try {
            // Create example data to sign.
            XmlDocument document = new XmlDocument();
            XmlNode node = document.CreateNode(XmlNodeType.Element, "",
 
                "MyElement", "samples");
            node.set_InnerText("This is some text");
            document.AppendChild(node);
            Console.WriteLine(("Data to sign:\n" + document.get_OuterXml()
 
                + "\n"));

            // Create the SignedXml message.
            SignedXml signedXml = new SignedXml();
            RSA key = RSA.Create();
            signedXml.set_SigningKey(key);

            // Create a data object to hold the data to sign.
            DataObject dataObject = new DataObject();

            dataObject.set_Data(document.get_ChildNodes());
            dataObject.set_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.set_Uri("#MyObjectId");

            // Add the reference to the message.
            signedXml.AddReference(reference);

            // Add a KeyInfo.
            KeyInfo keyInfo = new KeyInfo();

            keyInfo.AddClause(new RSAKeyValue(key));
            signedXml.set_KeyInfo(keyInfo);

            // Compute the signature.
            signedXml.ComputeSignature();
            Console.WriteLine("The data was signed.");
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //main
} //XMLdsigsample1
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SignedXml クラス
SignedXml メンバ
System.Security.Cryptography.Xml 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からSignedXml.AddObject メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からSignedXml.AddObject メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からSignedXml.AddObject メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

SignedXml.AddObject メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SignedXml.AddObject メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS