X509Certificate 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供帮助你使用 X.509 v.3 证书的方法。
public ref class X509Certificate : IDisposable, System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializablepublic ref class X509Certificate : IDisposablepublic ref class X509Certificatepublic ref class X509Certificate : System::Runtime::Serialization::IDeserializationCallback, System::Runtime::Serialization::ISerializablepublic class X509Certificate : IDisposable, System.Runtime.Serialization.IDeserializationCallback, System.Runtime.Serialization.ISerializablepublic 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.ISerializabletype X509Certificate = class
    interface IDisposable
    interface IDeserializationCallback
    interface ISerializabletype X509Certificate = class
    interface IDisposabletype 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 ISerializablePublic Class X509Certificate
Implements IDeserializationCallback, IDisposable, ISerializablePublic Class X509Certificate
Implements IDisposablePublic Class X509CertificatePublic Class X509Certificate
Implements IDeserializationCallback, ISerializable- 继承
- 
				X509Certificate
- 派生
- 属性
- 实现
示例
以下示例从文件加载 X.509 证书,调用 ToString 方法,并将结果显示到控制台。
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 是此类支持的唯一证书格式。
在大多数情况下,应改用 X509Certificate2 类。
重要
从 .NET Framework 4.6 开始,此类型实现 IDisposable 接口。 在使用完类型后,您应直接或间接释放类型。 若要直接释放类型,请在 try/catch 块中调用其 Dispose 方法。 若要间接释放类型,请使用 using(在 C# 中)或 Using(在 Visual Basic 中)等语言构造。 有关详细信息,请参阅 IDisposable 接口主题中的“使用实现 IDisposable 的对象”一节。
对于面向 .NET Framework 4.5.2 及更早版本的应用, X509Certificate 类不实现 IDisposable 接口,因此没有 Dispose 方法。
构造函数
属性
| Handle | 获取非托管  | 
| 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) | 以 X509Certificate 值之一所描述的格式将当前 X509ContentType 对象导出到字节数组。 | 
| Export(X509ContentType, SecureString) | 使用指定的格式和密码将当前 X509Certificate 对象导出到字节数组。 | 
| Export(X509ContentType, String) | 使用指定的密码,以 X509Certificate 值之一所描述的格式将当前 X509ContentType 对象导出到字节数组。 | 
| FormatDate(DateTime) | 将指定的日期和时间转换为字符串。 | 
| GetCertHash() | 将 X.509v3 证书的哈希值作为字节数组返回。 | 
| GetCertHash(HashAlgorithmName) | 返回使用指定加密哈希算法计算的 X.509v3 证书的哈希值。 | 
| GetCertHashString() | 以十六进制字符串的形式返回 X.509v3 证书的 SHA1 哈希值。 | 
| 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 证书的序列号以 little-endian 的顺序作为字节数组返回。 | 
| GetSerialNumberString() | 将 X.509v3 证书的序列号作为 little-endian 十六进制字符串返回。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| Import(Byte[]) | 
				已过时.
			 
				已过时.
			 使用字节数组中的数据填充 X509Certificate 对象。 | 
| Import(Byte[], SecureString, X509KeyStorageFlags) | 
				已过时.
			 
				已过时.
			 使用一个字节数组中的数据、一个密码和一个密钥存储标志填充 X509Certificate 对象。 | 
| Import(Byte[], String, X509KeyStorageFlags) | 
				已过时.
			 
				已过时.
			 使用一个字节数组中的数据、一个密码和用于确定如何导入私钥的标志填充 X509Certificate 对象。 | 
| Import(String) | 
				已过时.
			 
				已过时.
			 使用证书文件中的信息填充 X509Certificate 对象。 | 
| Import(String, SecureString, X509KeyStorageFlags) | 
				已过时.
			 
				已过时.
			 使用一个证书文件中的信息,一个密码和一个密钥存储标志填充 X509Certificate 对象。 | 
| Import(String, String, X509KeyStorageFlags) | 
				已过时.
			 
				已过时.
			 使用一个证书文件中的信息、一个密码和一个 X509Certificate 值填充 X509KeyStorageFlags 对象。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Reset() | 重置 X509Certificate2 对象的状态。 | 
| ToString() | 返回当前 X509Certificate 对象的字符串表示形式。 | 
| ToString(Boolean) | 返回当前 X509Certificate 对象的字符串表示形式,如果指定,带有其他信息。 | 
| TryGetCertHash(HashAlgorithmName, Span<Byte>, Int32) | 尝试为证书生成“指纹”,方法是使用指定的哈希算法对已编码的证书表示形式进行哈希处理。 | 
显式接口实现
| IDeserializationCallback.OnDeserialization(Object) | 实现 ISerializable 接口,并在完成反序列化后由反序列化事件回调。 | 
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) | 获取序列化信息,其中包含重新创建当前 X509Certificate 对象的实例所需的所有数据。 |