语言

X509Certificate 类

定义

提供有助于使用 X.509 v.3 证书的方法。

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

示例

以下示例从文件加载 X.509 证书,调用 ToString 该方法,并向控制台显示结果。

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 是唯一此类支持的证书格式。

对于大多数方案,应改用 X509Certificate2 类。

Important

从 .NET Framework 4.6 开始,此类型实现 IDisposable 接口。 使用完该类型后,应直接或间接处理它。 若要直接释放类型,请在块中Disposetry/调用其catch方法。 若要间接释放它,请使用语言构造,例如 using (在 C# 中)或 Using (在 Visual Basic 中)。 有关详细信息,请参阅接口主题中的 IDisposable “使用实现 IDisposable 的对象”部分。

对于面向 .NET Framework 4.5.2 及更低版本的应用,该 X509Certificate 类不实现 IDisposable 接口,因此没有方法 Dispose 。

构造函数

名称 说明
X509Certificate()
已过时.
已过时.

初始化 X509Certificate 类的新实例。

X509Certificate(Byte[], SecureString, X509KeyStorageFlags)
已过时.

使用字节数组、密码和密钥存储标志初始化类的新实例 X509Certificate 。

X509Certificate(Byte[], SecureString)
已过时.

使用字节数组和密码初始化类的新实例 X509Certificate 。

X509Certificate(Byte[], String, X509KeyStorageFlags)
已过时.

使用字节数组、密码和密钥存储标志初始化类的新实例 X509Certificate 。

X509Certificate(Byte[], String)
已过时.

使用字节数组和密码初始化类的新实例 X509Certificate 。

X509Certificate(Byte[])
已过时.

初始化从表示 X.509v3 证书的字节序列中定义的类的新实例 X509Certificate 。

X509Certificate(IntPtr)

使用非托管X509Certificate结构的句柄初始化类的新实例PCCERT_CONTEXT。

X509Certificate(SerializationInfo, StreamingContext)
已过时.

使用X509Certificate对象和SerializationInfo结构初始化类的新实例StreamingContext。

X509Certificate(String, SecureString, X509KeyStorageFlags)
已过时.

使用证书文件名、密码和密钥存储标志初始化类的新实例 X509Certificate 。

X509Certificate(String, SecureString)
已过时.

使用证书文件名和密码初始化类的新实例 X509Certificate 。

X509Certificate(String, String, X509KeyStorageFlags)
已过时.

使用 PKCS7 签名文件的名称、用于访问证书的密码和密钥存储标志初始化类的新实例 X509Certificate 。

X509Certificate(String, String)
已过时.

使用 PKCS7 签名文件的名称和用于访问证书的密码初始化类的新实例 X509Certificate 。

X509Certificate(String)
已过时.

使用 PKCS7 签名文件的名称初始化类的新实例 X509Certificate 。

X509Certificate(X509Certificate)

使用另一个X509Certificate类初始化类的新实例X509Certificate。

属性

名称 说明
Handle

获取由非托管 PCCERT_CONTEXT 结构描述的Microsoft加密 API 证书上下文的句柄。

Issuer

获取颁发 X.509v3 证书的证书颁发机构的名称。

SerialNumberBytes

获取证书序列号的 big-endian 表示形式。

Subject

从证书中获取使用者可分辨名称。

方法

名称 说明
CreateFromCertFile(String)
已过时.

从指定的 PKCS7 签名文件创建 X.509v3 证书。

CreateFromSignedFile(String)
已过时.

从指定的签名文件创建 X.509v3 证书。

Dispose()

释放当前 X509Certificate 对象使用的所有资源。

Dispose(Boolean)

释放此 X509Certificate 资源使用的所有非托管资源,并选择性地释放托管资源。

Equals(Object)

比较两个 X509Certificate 对象是否相等。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
Equals(X509Certificate)

比较两个 X509Certificate 对象是否相等。

Export(X509ContentType, SecureString)

使用指定的格式和密码将当前 X509Certificate 对象导出到字节数组。

Export(X509ContentType, String)

以值之一X509Certificate描述的格式将当前X509ContentType对象导出到字节数组,并使用指定的密码。

Export(X509ContentType)

以值之一X509Certificate描述的格式将当前X509ContentType对象导出到字节数组。

ExportPkcs12(PbeParameters, String)

以 PKCS#12 / PFX 格式导出证书和私钥。

ExportPkcs12(Pkcs12ExportPbeParameters, String)

以 PKCS#12 / PFX 格式导出证书和私钥。

FormatDate(DateTime)

将指定的日期和时间转换为字符串。

GetCertHash()

以字节数组的形式返回 X.509v3 证书的哈希值。

GetCertHash(HashAlgorithmName)

返回使用指定的加密哈希算法计算的 X.509v3 证书的哈希值。

GetCertHashString()

以十六进制字符串的形式返回 X.509v3 证书的 SHA-1 哈希值。

GetCertHashString(HashAlgorithmName)

返回一个十六进制字符串,其中包含使用指定的加密哈希算法计算的 X.509v3 证书的哈希值。

GetEffectiveDateString()

返回此 X.509v3 证书的有效日期。

GetExpirationDateString()

返回此 X.509v3 证书的到期日期。

GetFormat()

返回此 X.509v3 证书的格式的名称。

GetHashCode()

以整数的形式返回 X.509v3 证书的哈希代码。

GetIssuerName()
已过时.
已过时.
已过时.

返回颁发 X.509v3 证书的证书颁发机构的名称。

GetKeyAlgorithm()

以字符串的形式返回此 X.509v3 证书的密钥算法信息。

GetKeyAlgorithmParameters()

以字节数组的形式返回 X.509v3 证书的密钥算法参数。

GetKeyAlgorithmParametersString()

以十六进制字符串的形式返回 X.509v3 证书的密钥算法参数。

GetName()
已过时.
已过时.
已过时.

返回颁发证书的主体的名称。

GetPublicKey()

以字节数组的形式返回 X.509v3 证书的公钥。

GetPublicKeyString()

以十六进制字符串的形式返回 X.509v3 证书的公钥。

GetRawCertData()

以字节数组的形式返回整个 X.509v3 证书的原始数据。

GetRawCertDataString()

以十六进制字符串的形式返回整个 X.509v3 证书的原始数据。

GetSerialNumber()

以小端顺序返回 X.509v3 证书的序列号作为字节数组。

GetSerialNumberString()

以大端十六进制字符串形式返回 X.509v3 证书的序列号。

GetType()

获取当前实例的 Type。

(继承自 Object)
Import(Byte[], SecureString, X509KeyStorageFlags)
已过时.
已过时.

X509Certificate使用字节数组、密码和密钥存储标志中的数据填充对象。

Import(Byte[], String, X509KeyStorageFlags)
已过时.
已过时.

X509Certificate使用字节数组中的数据、密码和标志填充对象,以确定私钥的导入方式。

Import(Byte[])
已过时.
已过时.

X509Certificate使用字节数组中的数据填充对象。

Import(String, SecureString, X509KeyStorageFlags)
已过时.
已过时.

X509Certificate使用证书文件、密码和密钥存储标志中的信息填充对象。

Import(String, String, X509KeyStorageFlags)
已过时.
已过时.

X509Certificate使用证书文件、密码和X509KeyStorageFlags值中的信息填充对象。

Import(String)
已过时.
已过时.

X509Certificate使用证书文件中的信息填充对象。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Reset()

重置对象的状态 X509Certificate2 。

ToString()

返回当前 X509Certificate 对象的字符串表示形式。

ToString(Boolean)

返回当前 X509Certificate 对象的字符串表示形式,如果指定了额外的信息。

TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32)

尝试使用指定的哈希算法对证书的编码表示形式进行哈希处理,从而为证书生成“指纹”。

显式接口实现

名称 说明
IDeserializationCallback.OnDeserialization(Object)

实现接口, ISerializable 并在反序列化完成后由反序列化事件调用。

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

获取序列化信息,其中包含重新创建当前 X509Certificate 对象的实例所需的所有数据。

适用于