X509Certificate2.Import メソッド

定義

提供された証明書情報を使用して、X509Certificate2 オブジェクトを設定します。

オーバーロード

Import(Byte[])
古い.

バイト配列のデータを使用して、X509Certificate2 オブジェクトを設定します。

Import(String)
古い.

証明書ファイルからの情報を使用して、X509Certificate2 オブジェクトを設定します。

Import(Byte[], SecureString, X509KeyStorageFlags)
古い.

バイト配列のデータ、パスワード、およびキー格納フラグを使用して、X509Certificate2 オブジェクトを設定します。

Import(Byte[], String, X509KeyStorageFlags)
古い.

バイト配列のデータ、パスワード、および秘密キーのインポート方法を決定するフラグを使用して、X509Certificate2 オブジェクトを設定します。

Import(String, SecureString, X509KeyStorageFlags)
古い.

証明書ファイルの情報、パスワード、およびキー格納フラグを指定して、X509Certificate2 オブジェクトを設定します。

Import(String, String, X509KeyStorageFlags)
古い.

証明書ファイルの情報、パスワード、および X509Certificate2 値を使用して、X509KeyStorageFlags オブジェクトを設定します。

Import(Byte[])

注意事項

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

バイト配列のデータを使用して、X509Certificate2 オブジェクトを設定します。

public:
 override void Import(cli::array <System::Byte> ^ rawData);
public override void Import (byte[] rawData);
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override void Import (byte[] rawData);
override this.Import : byte[] -> unit
[<System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Import : byte[] -> unit
Public Overrides Sub Import (rawData As Byte())

パラメーター

rawData
Byte[]

X.509 証明書のデータを格納しているバイト配列。

属性

注釈

このメソッドを使用すると、X.509 証明書の生のバイト配列を取得し、オブジェクトに関連する値を X509Certificate2 設定できます。

このメソッドはバイト配列のみを受け入れるため、Base64 でエンコードされた X.509 証明書や PFX/PKCS12 証明書など、パスワードを必要としない証明書の種類にのみ使用できます。 PFX/PKCS12 証明書には複数の証明書を含めることができます。 その場合、秘密キーに関連付けられている最初の証明書が使用されます。または、秘密キーが見つからない場合は、最初の証明書が使用されます。

適用対象

Import(String)

注意事項

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

証明書ファイルからの情報を使用して、X509Certificate2 オブジェクトを設定します。

public:
 override void Import(System::String ^ fileName);
public override void Import (string fileName);
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override void Import (string fileName);
override this.Import : string -> unit
[<System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Import : string -> unit
Public Overrides Sub Import (fileName As String)

パラメーター

fileName
String

証明書の名前。

属性

注釈

このメソッドは、X.509 証明書を表し、ファイルに含まれる証明書をオブジェクトに設定 X509Certificate2 する証明書ファイル (拡張子が .cer のファイルなど) を使用します。

この方法は、Base64 でエンコードされた証明書または DER でエンコードされた X.509 証明書、PFX/PKCS12 証明書、Authenticode などの署名者証明書など、いくつかの証明書の種類で使用できます。 PFX/PKCS12 証明書には複数の証明書を含めることができます。 その場合、秘密キーに関連付けられている最初の証明書が使用されます。または、秘密キーが見つからない場合は、最初の証明書が使用されます。

適用対象

Import(Byte[], SecureString, X509KeyStorageFlags)

注意事項

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

重要

この API は CLS 準拠ではありません。

バイト配列のデータ、パスワード、およびキー格納フラグを使用して、X509Certificate2 オブジェクトを設定します。

public:
 override void Import(cli::array <System::Byte> ^ rawData, System::Security::SecureString ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
public override void Import (byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override void Import (byte[] rawData, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
public override void Import (byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[<System.CLSCompliant(false)>]
override this.Import : byte[] * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
[<System.CLSCompliant(false)>]
[<System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Import : byte[] * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
override this.Import : byte[] * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (rawData As Byte(), password As SecureString, keyStorageFlags As X509KeyStorageFlags)

パラメーター

rawData
Byte[]

X.509 証明書のデータを格納しているバイト配列。

password
SecureString

X.509 証明書データにアクセスするために必要なパスワード。

keyStorageFlags
X509KeyStorageFlags

証明書をインポートする場所と方法を制御する列挙値のビットごとの組み合わせ。

属性

注釈

この方法は、Base64 でエンコードされた証明書や DER でエンコードされた X.509 証明書、PFX/PKCS12 証明書などの証明書の種類に使用できます。 PFX/PKCS12 証明書には複数の証明書を含めることができます。 その場合、秘密キーに関連付けられている最初の証明書が使用されます。または、秘密キーが見つからない場合は、最初の証明書が使用されます。

重要

ソース コード内でパスワードをハードコードしないでください。 ハードコードされたパスワードは、Ildasm.exe (IL Disassembler)、16 進エディター、または Notepad.exe などのテキスト エディターでアセンブリを開くだけで取得することが可能です。

適用対象

Import(Byte[], String, X509KeyStorageFlags)

注意事項

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

バイト配列のデータ、パスワード、および秘密キーのインポート方法を決定するフラグを使用して、X509Certificate2 オブジェクトを設定します。

public:
 override void Import(cli::array <System::Byte> ^ rawData, System::String ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override void Import (byte[] rawData, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (byte[] rawData, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
override this.Import : byte[] * string * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
[<System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Import : byte[] * string * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (rawData As Byte(), password As String, keyStorageFlags As X509KeyStorageFlags)

パラメーター

rawData
Byte[]

X.509 証明書のデータを格納しているバイト配列。

password
String

X.509 証明書データにアクセスするために必要なパスワード。

keyStorageFlags
X509KeyStorageFlags

証明書をインポートする場所と方法を制御する列挙値のビットごとの組み合わせ。

属性

次のコード例では、証明書ファイルを引数として受け取り、さまざまな証明書プロパティをコンソールに出力するコマンド ライン実行可能ファイルを作成します。

#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Permissions;
using namespace System::IO;
using namespace System::Security::Cryptography::X509Certificates;

//Reads a file.
array<Byte>^ ReadFile( String^ fileName )
{
   FileStream^ f = gcnew FileStream( fileName,FileMode::Open,FileAccess::Read );
   int size = (int)f->Length;
   array<Byte>^data = gcnew array<Byte>(size);
   size = f->Read( data, 0, size );
   f->Close();
   return data;
}

[SecurityPermissionAttribute(SecurityAction::LinkDemand, Unrestricted = true)]
int main()
{
   array<String^>^args = Environment::GetCommandLineArgs();

   //Test for correct number of arguments.
   if ( args->Length < 2 )
   {
      Console::WriteLine( "Usage: CertInfo <filename>" );
      return  -1;
   }

   try
   {
      System::Security::Cryptography::X509Certificates::X509Certificate2 ^ x509 =
            gcnew System::Security::Cryptography::X509Certificates::X509Certificate2;

      //Create X509Certificate2 object from .cer file.
      array<Byte>^rawData = ReadFile( args[ 1 ] );

      x509->Import(rawData);

      //Print to console information contained in the certificate.
      Console::WriteLine( "{0}Subject: {1}{0}", Environment::NewLine, x509->Subject );
      Console::WriteLine( "{0}Issuer: {1}{0}", Environment::NewLine, x509->Issuer );
      Console::WriteLine( "{0}Version: {1}{0}", Environment::NewLine, x509->Version );
      Console::WriteLine( "{0}Valid Date: {1}{0}", Environment::NewLine, x509->NotBefore );
      Console::WriteLine( "{0}Expiry Date: {1}{0}", Environment::NewLine, x509->NotAfter );
      Console::WriteLine( "{0}Thumbprint: {1}{0}", Environment::NewLine, x509->Thumbprint );
      Console::WriteLine( "{0}Serial Number: {1}{0}", Environment::NewLine, x509->SerialNumber );
      Console::WriteLine( "{0}Friendly Name: {1}{0}", Environment::NewLine, x509->PublicKey->Oid->FriendlyName );
      Console::WriteLine( "{0}Public Key Format: {1}{0}", Environment::NewLine, x509->PublicKey->EncodedKeyValue->Format(true) );
      Console::WriteLine( "{0}Raw Data Length: {1}{0}", Environment::NewLine, x509->RawData->Length );
      Console::WriteLine( "{0}Certificate to string: {1}{0}", Environment::NewLine, x509->ToString( true ) );
      Console::WriteLine( "{0}Certificate to XML String: {1}{0}", Environment::NewLine, x509->PublicKey->Key->ToXmlString( false ) );

      //Add the certificate to a X509Store.
      X509Store ^ store = gcnew X509Store;
      store->Open( OpenFlags::MaxAllowed );
      store->Add( x509 );
      store->Close();
   }
   catch ( DirectoryNotFoundException^ )
   {
      Console::WriteLine( "Error: The directory specified could not be found." );
   }
   catch ( IOException^ )
   {
      Console::WriteLine( "Error: A file in the directory could not be accessed." );
   }
   catch ( NullReferenceException^ )
   {
      Console::WriteLine( "File must be a .cer file. Program does not have access to that type of file." );
   }

}
using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

class CertInfo
{
    //Reads a file.
    internal static byte[] ReadFile (string fileName)
    {
        FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        int size = (int)f.Length;
        byte[] data = new byte[size];
        size = f.Read(data, 0, size);
        f.Close();
        return data;
    }
    //Main method begins here.
    static void Main(string[] args)
    {
        //Test for correct number of arguments.
        if (args.Length < 1)
        {
            Console.WriteLine("Usage: CertInfo <filename>");
            return;
        }
        try
        {
            X509Certificate2 x509 = new X509Certificate2();
            //Create X509Certificate2 object from .cer file.
            byte[] rawData = ReadFile(args[0]);
            x509.Import(rawData);

            //Print to console information contained in the certificate.
            Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject);
            Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer);
            Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version);
            Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore);
            Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter);
            Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint);
            Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber);
            Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName);
            Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(true));
            Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length);
            Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(true));
            Console.WriteLine("{0}Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(false));

            //Add the certificate to a X509Store.
            X509Store store = new X509Store();
            store.Open(OpenFlags.MaxAllowed);
            store.Add(x509);
            store.Close();
        }
        catch (DirectoryNotFoundException)
        {
               Console.WriteLine("Error: The directory specified could not be found.");
        }
        catch (IOException)
        {
            Console.WriteLine("Error: A file in the directory could not be accessed.");
        }
        catch (NullReferenceException)
        {
            Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.");
        }
    }
}
Imports System.Security.Cryptography
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Cryptography.X509Certificates

Class CertInfo

    'Reads a file.
    Friend Shared Function ReadFile(ByVal fileName As String) As Byte()
        Dim f As New FileStream(fileName, FileMode.Open, FileAccess.Read)
        Dim size As Integer = Fix(f.Length)
        Dim data(size - 1) As Byte
        size = f.Read(data, 0, size)
        f.Close()
        Return data

    End Function 

    <SecurityPermission(SecurityAction.LinkDemand, Unrestricted:=True)> _
    Shared Sub Main(ByVal args() As String)
        'Test for correct number of arguments.
        If args.Length < 1 Then
            Console.WriteLine("Usage: CertInfo <filename>")
            Return
        End If
        Try
            Dim x509 As New X509Certificate2()
            'Create X509Certificate2 object from .cer file.
            Dim rawData As Byte() = ReadFile(args(0))
            
            x509.Import(rawData)

            'Print to console information contained in the certificate.
            Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject)
            Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer)
            Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version)
            Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore)
            Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter)
            Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint)
            Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber)
            Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName)
            Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(True))
            Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length)
            Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(True))

            Console.WriteLine("{0}Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(False))

            'Add the certificate to a X509Store.
            Dim store As New X509Store()
            store.Open(OpenFlags.MaxAllowed)
            store.Add(x509)
            store.Close()

        Catch dnfExcept As DirectoryNotFoundException
            Console.WriteLine("Error: The directory specified could not be found.")
        Catch ioExpcept As IOException
            Console.WriteLine("Error: A file in the directory could not be accessed.")
        Catch nrExcept As NullReferenceException
            Console.WriteLine("File must be a .cer file. Program does not have access to that type of file.")
        End Try

    End Sub
End Class

注釈

このメソッドを使用すると、バイト配列で表される証明書のパスワードを使用してオブジェクトを設定 X509Certificate2 できます。 値を X509KeyStorageFlags 使用して、秘密キーをインポートする場所と方法を制御できます。

このメソッドはバイト配列を受け入れ、Base64 でエンコードされた証明書や DER でエンコードされた X.509 証明書や PFX/PKCS12 証明書などの証明書の種類に使用できます。 PFX/PKCS12 証明書には複数の証明書を含めることができます。 その場合、秘密キーに関連付けられている最初の証明書が使用されます。または、秘密キーが見つからない場合は、最初の証明書が使用されます。

重要

ソース コード内でパスワードをハードコードしないでください。 ハードコードされたパスワードは、Ildasm.exe (IL Disassembler)、16 進エディター、または Notepad.exe などのテキスト エディターでアセンブリを開くだけで取得することが可能です。

適用対象

Import(String, SecureString, X509KeyStorageFlags)

注意事項

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

重要

この API は CLS 準拠ではありません。

証明書ファイルの情報、パスワード、およびキー格納フラグを指定して、X509Certificate2 オブジェクトを設定します。

public:
 override void Import(System::String ^ fileName, System::Security::SecureString ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
public override void Import (string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override void Import (string fileName, System.Security.SecureString? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.CLSCompliant(false)]
public override void Import (string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, System.Security.SecureString password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[<System.CLSCompliant(false)>]
override this.Import : string * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
[<System.CLSCompliant(false)>]
[<System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Import : string * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
override this.Import : string * System.Security.SecureString * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (fileName As String, password As SecureString, keyStorageFlags As X509KeyStorageFlags)

パラメーター

fileName
String

証明書ファイルの名前。

password
SecureString

X.509 証明書データにアクセスするために必要なパスワード。

keyStorageFlags
X509KeyStorageFlags

証明書をインポートする場所と方法を制御する列挙値のビットごとの組み合わせ。

属性

注釈

セキュリティに関する注意 ソース コード内でパスワードをハード コーディングしないでください。 ハードコードされたパスワードは、Ildasm.exe (IL Disassembler)、16 進エディター、または Notepad.exe などのテキスト エディターでアセンブリを開くだけで取得することが可能です。

適用対象

Import(String, String, X509KeyStorageFlags)

注意事項

X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.

証明書ファイルの情報、パスワード、および X509Certificate2 値を使用して、X509KeyStorageFlags オブジェクトを設定します。

public:
 override void Import(System::String ^ fileName, System::String ^ password, System::Security::Cryptography::X509Certificates::X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
[System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public override void Import (string fileName, string? password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
public override void Import (string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags);
override this.Import : string * string * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
[<System.Obsolete("X509Certificate and X509Certificate2 are immutable. Use the appropriate constructor to create a new certificate.", DiagnosticId="SYSLIB0026", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Import : string * string * System.Security.Cryptography.X509Certificates.X509KeyStorageFlags -> unit
Public Overrides Sub Import (fileName As String, password As String, keyStorageFlags As X509KeyStorageFlags)

パラメーター

fileName
String

証明書ファイルの名前。

password
String

X.509 証明書データにアクセスするために必要なパスワード。

keyStorageFlags
X509KeyStorageFlags

証明書をインポートする場所と方法を制御する列挙値のビットごとの組み合わせ。

属性

注釈

このメソッドを使用すると、証明書ファイル、パスワード、および値からの情報をオブジェクトにX509KeyStorageFlags設定X509Certificate2できます。

重要

ソース コード内でパスワードをハードコードしないでください。 ハードコードされたパスワードは、Ildasm.exe (IL Disassembler)、16 進エディター、または Notepad.exe などのテキスト エディターでアセンブリを開くだけで取得することが可能です。

適用対象