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[])

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

注意

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 编码或 DER 编码的 X.509 证书或 PFX/PKCS12 证书。 请注意,PFX/PKCS12 证书可以包含多个证书。 在这种情况下,将使用与私钥关联的第一个证书,如果未找到私钥,则使用第一个证书。

适用于

Import(String)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

注意

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

证书的名称。

属性

注解

此方法使用证书文件(例如扩展名为 .cer 的文件)表示 X.509 证书,并使用文件包含的证书填充 X509Certificate2 对象。

此方法可用于多种证书类型,包括 Base64 编码或 DER 编码的 X.509 证书、PFX/PKCS12 证书和签名者证书(如 Authenticode)。 请注意,PFX/PKCS12 证书可以包含多个证书。 在这种情况下,将使用与私钥关联的第一个证书,如果未找到私钥,则使用第一个证书。

适用于

Import(Byte[], SecureString, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

注意

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 反汇编程序) (十六进制编辑器)或直接在文本编辑器(如 Notepad.exe)中打开程序集来检索硬编码密码。

适用于

Import(Byte[], String, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

注意

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 反汇编程序) (十六进制编辑器)或直接在文本编辑器(如 Notepad.exe)中打开程序集来检索硬编码密码。

适用于

Import(String, SecureString, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

注意

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 反汇编程序) (十六进制编辑器)或直接在文本编辑器(如 Notepad.exe)中打开程序集来检索硬编码密码。

适用于

Import(String, String, X509KeyStorageFlags)

Source:
X509Certificate2.cs
Source:
X509Certificate2.cs
Source:
X509Certificate2.cs

注意

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 反汇编程序) (十六进制编辑器)或直接在文本编辑器(如 Notepad.exe)中打开程序集来检索硬编码密码。

适用于