X509Certificate2.Import 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
用提供的憑證資訊,填入 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 編碼或 DER 編碼 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
憑證的名稱。
- 屬性
備註
這個方法會使用憑證檔案,例如副檔名為 .cer 的檔案,代表 X.509 憑證,並以檔案所包含的憑證填 X509Certificate2 入 物件。
這個方法可以搭配數種憑證類型使用,包括 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 反組譯程式) 、十六進位編輯器,或在文字編輯器中開啟元件,例如 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 反組譯程式) 、十六進位編輯器,或在文字編輯器中開啟元件,例如 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 反組譯程式) 、十六進位編輯器,或在文字編輯器中開啟元件,例如 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 反組譯程式) 、十六進位編輯器,或在文字編輯器中開啟元件,例如 Notepad.exe,從元件擷取。