X509Certificate X509Certificate X509Certificate X509Certificate Class

定义

提供帮助你使用 X.509 v.3 证书的方法。Provides methods that help you use X.509 v.3 certificates.

public ref class X509Certificate : IDisposable, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class X509Certificate : IDisposable, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializable
type X509Certificate = class
    interface IDeserializationCallback
    interface ISerializable
    interface IDisposable
Public Class X509Certificate
Implements IDeserializationCallback, IDisposable, ISerializable
继承
X509CertificateX509CertificateX509CertificateX509Certificate
派生
属性
实现

示例

下面的示例从文件中加载 x.509 证书,调用ToString方法,并将结果显示到控制台。The following example loads an X.509 certificate from a file, calls the ToString method, and displays the results to the console.

using namespace System;
using namespace System::Security::Cryptography::X509Certificates;
int main()
{
   
   // The path to the certificate.
   String^ Certificate = L"Certificate.cer";
   
   // Load the certificate into an X509Certificate object.
   X509Certificate^ cert = gcnew X509Certificate( Certificate );
   
   // Get the value.
   String^ resultsTrue = cert->ToString( true );
   
   // Display the value to the console.
   Console::WriteLine( resultsTrue );
   
   // Get the value.
   String^ resultsFalse = cert->ToString( false );
   
   // Display the value to the console.
   Console::WriteLine( resultsFalse );
}

using System;
using System.Security.Cryptography.X509Certificates;


public class X509
{

    public static void Main()
    {

        // The path to the certificate.
        string Certificate = "Certificate.cer";

        // Load the certificate into an X509Certificate object.
        X509Certificate cert = new X509Certificate(Certificate);

        // Get the value.
        string resultsTrue = cert.ToString(true);

        // Display the value to the console.
        Console.WriteLine(resultsTrue);

        // Get the value.
        string resultsFalse = cert.ToString(false);

        // Display the value to the console.
        Console.WriteLine(resultsFalse);

    }

}
Imports System.Security.Cryptography.X509Certificates

Module X509

    Sub Main()

        ' The path to the certificate.
        Dim Certificate As String = "Certificate.cer"

        ' Load the certificate into an X509Certificate object.
        Dim cert As New X509Certificate(Certificate)

        ' Get the value.
        Dim resultsTrue As String = cert.ToString(True)

        ' Display the value to the console.
        Console.WriteLine(resultsTrue)

        ' Get the value.
        Dim resultsFalse As String = cert.ToString(False)

        ' Display the value to the console.
        Console.WriteLine(resultsFalse)

    End Sub
End Module

注解

只有此类支持的唯一证书格式。ASN.1 DER is the only certificate format supported by this class.

在大多数情况下,应该X509Certificate2改用类。For most scenarios, you should use the X509Certificate2 class instead.

重要

从开始IDisposable ,此类型实现接口。 .NET Framework 4.6.NET Framework 4.6Starting with the .NET Framework 4.6.NET Framework 4.6, this type implements the IDisposable interface. 在使用完类型后,您应直接或间接释放类型。When you have finished using the type, you should dispose of it either directly or indirectly. 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。To dispose of the type directly, call its Dispose method in a try/catch block. 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

对于面向.NET Framework 4.5.2.NET Framework 4.5.2和更早版本的应用X509CertificateIDisposable类不实现接口,因此不具有Dispose方法。For apps that target the .NET Framework 4.5.2.NET Framework 4.5.2 and earlier versions, the X509Certificate class does not implement the IDisposable interface and therefore does not have a Dispose method.

构造函数

X509Certificate() X509Certificate() X509Certificate() X509Certificate()

初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class.

X509Certificate(Byte[]) X509Certificate(Byte[]) X509Certificate(Byte[]) X509Certificate(Byte[])

初始化从表示 X.509v3 证书的字节序列定义的 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class defined from a sequence of bytes representing an X.509v3 certificate.

X509Certificate(Byte[], SecureString) X509Certificate(Byte[], SecureString) X509Certificate(Byte[], SecureString) X509Certificate(Byte[], SecureString)

使用一个字节数组和一个密码初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using a byte array and a password.

X509Certificate(Byte[], SecureString, X509KeyStorageFlags) X509Certificate(Byte[], SecureString, X509KeyStorageFlags) X509Certificate(Byte[], SecureString, X509KeyStorageFlags) X509Certificate(Byte[], SecureString, X509KeyStorageFlags)

使用一个字节数组、一个密码和一个密钥存储标志初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.

X509Certificate(Byte[], String) X509Certificate(Byte[], String) X509Certificate(Byte[], String) X509Certificate(Byte[], String)

使用一个字节数组和一个密码初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using a byte array and a password.

X509Certificate(Byte[], String, X509KeyStorageFlags) X509Certificate(Byte[], String, X509KeyStorageFlags) X509Certificate(Byte[], String, X509KeyStorageFlags) X509Certificate(Byte[], String, X509KeyStorageFlags)

使用一个字节数组、一个密码和一个密钥存储标志初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using a byte array, a password, and a key storage flag.

X509Certificate(IntPtr) X509Certificate(IntPtr) X509Certificate(IntPtr) X509Certificate(IntPtr)

使用非托管 PCCERT_CONTEXT 结构的句柄初始化 X509Certificate 类的一个新实例。Initializes a new instance of the X509Certificate class using a handle to an unmanaged PCCERT_CONTEXT structure.

X509Certificate(SerializationInfo, StreamingContext) X509Certificate(SerializationInfo, StreamingContext) X509Certificate(SerializationInfo, StreamingContext) X509Certificate(SerializationInfo, StreamingContext)

使用一个 X509Certificate 对象和一个 SerializationInfo 结构初始化 StreamingContext 类的一个新实例。Initializes a new instance of the X509Certificate class using a SerializationInfo object and a StreamingContext structure.

X509Certificate(String) X509Certificate(String) X509Certificate(String) X509Certificate(String)

使用 PKCS7 签名文件的名称初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file.

X509Certificate(String, SecureString) X509Certificate(String, SecureString) X509Certificate(String, SecureString) X509Certificate(String, SecureString)

使用一个证书文件名和一个密码初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using a certificate file name and a password.

X509Certificate(String, SecureString, X509KeyStorageFlags) X509Certificate(String, SecureString, X509KeyStorageFlags) X509Certificate(String, SecureString, X509KeyStorageFlags) X509Certificate(String, SecureString, X509KeyStorageFlags)

使用一个证书文件名、一个密码和一个密钥存储标志初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using a certificate file name, a password, and a key storage flag.

X509Certificate(String, String) X509Certificate(String, String) X509Certificate(String, String) X509Certificate(String, String)

使用 PKCS7 签名文件的名称和一个用于访问该证书的密码初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file and a password to access the certificate.

X509Certificate(String, String, X509KeyStorageFlags) X509Certificate(String, String, X509KeyStorageFlags) X509Certificate(String, String, X509KeyStorageFlags) X509Certificate(String, String, X509KeyStorageFlags)

使用 PKCS7 签名文件的名称、一个用于访问该证书的密码和一个密钥存储标志初始化 X509Certificate 类的新实例。Initializes a new instance of the X509Certificate class using the name of a PKCS7 signed file, a password to access the certificate, and a key storage flag.

X509Certificate(X509Certificate) X509Certificate(X509Certificate) X509Certificate(X509Certificate) X509Certificate(X509Certificate)

使用另一个 X509Certificate 类初始化 X509Certificate 类的一个新实例。Initializes a new instance of the X509Certificate class using another X509Certificate class.

属性

Handle Handle Handle Handle

获取非托管 PCCERT_CONTEXT 结构所描述的 Microsoft Cryptographic API 证书上下文的句柄。Gets a handle to a Microsoft Cryptographic API certificate context described by an unmanaged PCCERT_CONTEXT structure.

Issuer Issuer Issuer Issuer

获取颁发此 X.509v3 证书的证书颁发机构的名称。Gets the name of the certificate authority that issued the X.509v3 certificate.

Subject Subject Subject Subject

获取证书的主题可分辨名称。Gets the subject distinguished name from the certificate.

方法

CreateFromCertFile(String) CreateFromCertFile(String) CreateFromCertFile(String) CreateFromCertFile(String)

依据指定的 PKCS7 签名文件创建 X.509v3 证书。Creates an X.509v3 certificate from the specified PKCS7 signed file.

CreateFromSignedFile(String) CreateFromSignedFile(String) CreateFromSignedFile(String) CreateFromSignedFile(String)

依据指定的签名文件创建 X.509v3 证书。Creates an X.509v3 certificate from the specified signed file.

Dispose() Dispose() Dispose() Dispose()

释放由当前 X509Certificate 对象使用的所有资源。Releases all resources used by the current X509Certificate object.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放此 X509Certificate 使用的所有非托管资源,并且可选择释放托管资源。Releases all of the unmanaged resources used by this X509Certificate and optionally releases the managed resources.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

比较两个 X509Certificate 对象是否相等。Compares two X509Certificate objects for equality.

Equals(X509Certificate) Equals(X509Certificate) Equals(X509Certificate) Equals(X509Certificate)

比较两个 X509Certificate 对象是否相等。Compares two X509Certificate objects for equality.

Export(X509ContentType) Export(X509ContentType) Export(X509ContentType) Export(X509ContentType)

X509Certificate 值之一所描述的格式将当前 X509ContentType 对象导出到字节数组。Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values.

Export(X509ContentType, SecureString) Export(X509ContentType, SecureString) Export(X509ContentType, SecureString) Export(X509ContentType, SecureString)

使用指定的格式和密码将当前 X509Certificate 对象导出到字节数组。Exports the current X509Certificate object to a byte array using the specified format and a password.

Export(X509ContentType, String) Export(X509ContentType, String) Export(X509ContentType, String) Export(X509ContentType, String)

使用指定的密码,以 X509Certificate 值之一所描述的格式将当前 X509ContentType 对象导出到字节数组。Exports the current X509Certificate object to a byte array in a format described by one of the X509ContentType values, and using the specified password.

FormatDate(DateTime) FormatDate(DateTime) FormatDate(DateTime) FormatDate(DateTime)

将指定的日期和时间转换为字符串。Converts the specified date and time to a string.

GetCertHash() GetCertHash() GetCertHash() GetCertHash()

将 X.509v3 证书的哈希值作为字节数组返回。Returns the hash value for the X.509v3 certificate as an array of bytes.

GetCertHash(HashAlgorithmName) GetCertHash(HashAlgorithmName) GetCertHash(HashAlgorithmName) GetCertHash(HashAlgorithmName)

返回使用指定加密哈希算法计算的 X.509v3 证书的哈希值。Returns the hash value for the X.509v3 certificate that is computed by using the specified cryptographic hash algorithm.

GetCertHashString() GetCertHashString() GetCertHashString() GetCertHashString()

将 X.509v3 证书的 SHA1 哈希值作为十六进制字符串返回。Returns the SHA1 hash value for the X.509v3 certificate as a hexadecimal string.

GetCertHashString(HashAlgorithmName) GetCertHashString(HashAlgorithmName) GetCertHashString(HashAlgorithmName) GetCertHashString(HashAlgorithmName)

返回包含使用指定加密哈希算法计算的 X.509v3 证书的哈希值的十六进制字符串。Returns a hexadecimal string containing the hash value for the X.509v3 certificate computed using the specified cryptographic hash algorithm.

GetEffectiveDateString() GetEffectiveDateString() GetEffectiveDateString() GetEffectiveDateString()

返回此 X.509v3 证书的有效日期。Returns the effective date of this X.509v3 certificate.

GetExpirationDateString() GetExpirationDateString() GetExpirationDateString() GetExpirationDateString()

返回此 X.509v3 证书的到期日期。Returns the expiration date of this X.509v3 certificate.

GetFormat() GetFormat() GetFormat() GetFormat()

返回此 X.509v3 证书的格式的名称。Returns the name of the format of this X.509v3 certificate.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回整数形式的 X.509v3 证书的哈希代码。Returns the hash code for the X.509v3 certificate as an integer.

GetIssuerName() GetIssuerName() GetIssuerName() GetIssuerName()

返回颁发此 X.509v3 证书的证书颁发机构的名称。Returns the name of the certification authority that issued the X.509v3 certificate.

GetKeyAlgorithm() GetKeyAlgorithm() GetKeyAlgorithm() GetKeyAlgorithm()

将此 X.509v3 证书的密钥算法信息作为字符串返回。Returns the key algorithm information for this X.509v3 certificate as a string.

GetKeyAlgorithmParameters() GetKeyAlgorithmParameters() GetKeyAlgorithmParameters() GetKeyAlgorithmParameters()

将 X.509v3 证书的密钥算法参数作为字节数组返回。Returns the key algorithm parameters for the X.509v3 certificate as an array of bytes.

GetKeyAlgorithmParametersString() GetKeyAlgorithmParametersString() GetKeyAlgorithmParametersString() GetKeyAlgorithmParametersString()

将 X.509v3 证书的密钥算法参数作为十六进制字符串返回。Returns the key algorithm parameters for the X.509v3 certificate as a hexadecimal string.

GetName() GetName() GetName() GetName()

返回已向其颁发证书的主体的名称。Returns the name of the principal to which the certificate was issued.

GetPublicKey() GetPublicKey() GetPublicKey() GetPublicKey()

将 X.509v3 证书的公钥作为字节数组返回。Returns the public key for the X.509v3 certificate as an array of bytes.

GetPublicKeyString() GetPublicKeyString() GetPublicKeyString() GetPublicKeyString()

将 X.509v3 证书的公钥作为十六进制字符串返回。Returns the public key for the X.509v3 certificate as a hexadecimal string.

GetRawCertData() GetRawCertData() GetRawCertData() GetRawCertData()

将整个 X.509v3 证书的原始数据作为字节数组返回。Returns the raw data for the entire X.509v3 certificate as an array of bytes.

GetRawCertDataString() GetRawCertDataString() GetRawCertDataString() GetRawCertDataString()

将整个 X.509v3 证书的原始数据作为十六进制字符串返回。Returns the raw data for the entire X.509v3 certificate as a hexadecimal string.

GetSerialNumber() GetSerialNumber() GetSerialNumber() GetSerialNumber()

将 X.509v3 证书的序列号以 little-endian 的顺序作为字节数组返回。Returns the serial number of the X.509v3 certificate as an array of bytes in little-endian order.

GetSerialNumberString() GetSerialNumberString() GetSerialNumberString() GetSerialNumberString()

将 X.509v3 证书的序列号作为 little-endian 十六进制字符串返回。Returns the serial number of the X.509v3 certificate as a little-endian hexadecimal string .

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
Import(Byte[]) Import(Byte[]) Import(Byte[]) Import(Byte[])

使用字节数组中的数据填充 X509Certificate 对象。Populates the X509Certificate object with data from a byte array.

Import(Byte[], SecureString, X509KeyStorageFlags) Import(Byte[], SecureString, X509KeyStorageFlags) Import(Byte[], SecureString, X509KeyStorageFlags) Import(Byte[], SecureString, X509KeyStorageFlags)

使用一个字节数组中的数据、一个密码和一个密钥存储标志填充 X509Certificate 对象。Populates an X509Certificate object using data from a byte array, a password, and a key storage flag.

Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags) Import(Byte[], String, X509KeyStorageFlags)

使用一个字节数组中的数据、一个密码和用于确定如何导入私钥的标志填充 X509Certificate 对象。Populates the X509Certificate object using data from a byte array, a password, and flags for determining how the private key is imported.

Import(String) Import(String) Import(String) Import(String)

使用证书文件中的信息填充 X509Certificate 对象。Populates the X509Certificate object with information from a certificate file.

Import(String, SecureString, X509KeyStorageFlags) Import(String, SecureString, X509KeyStorageFlags) Import(String, SecureString, X509KeyStorageFlags) Import(String, SecureString, X509KeyStorageFlags)

使用一个证书文件中的信息,一个密码和一个密钥存储标志填充 X509Certificate 对象。Populates an X509Certificate object with information from a certificate file, a password, and a key storage flag.

Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags) Import(String, String, X509KeyStorageFlags)

使用一个证书文件中的信息、一个密码和一个 X509Certificate 值填充 X509KeyStorageFlags 对象。Populates the X509Certificate object with information from a certificate file, a password, and a X509KeyStorageFlags value.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Reset() Reset() Reset() Reset()

重置 X509Certificate2 对象的状态。Resets the state of the X509Certificate2 object.

ToString() ToString() ToString() ToString()

返回当前 X509Certificate 对象的字符串表示形式。Returns a string representation of the current X509Certificate object.

ToString(Boolean) ToString(Boolean) ToString(Boolean) ToString(Boolean)

返回当前 X509Certificate 对象的字符串表示形式,如果指定,带有其他信息。Returns a string representation of the current X509Certificate object, with extra information, if specified.

TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32)

显式界面实现

IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object) IDeserializationCallback.OnDeserialization(Object)

实现 ISerializable 接口,并在完成反序列化后由反序列化事件回调。Implements the ISerializable interface and is called back by the deserialization event when deserialization is complete.

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

获取序列化信息,其中包含重新创建当前 X509Certificate 对象的实例所需的所有数据。Gets serialization information with all the data needed to recreate an instance of the current X509Certificate object.

适用于