C#获取数字证书

string Thumbprint = "C2489D912F247C187AA14B1291A6fB612281225D";
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = 
  store.Certificates.Find(X509FindType.FindByThumbprint,
Thumbprint, false);

1、X509Store:表示 X.509 存储区,该存储区是保留和管理证书的物理存储区。 此类不能被继承。

   //
    // 摘要:
    //     表示 X.509 存储区,该存储区是保留和管理证书的物理存储区。 此类不能被继承。
    public sealed class X509Store
    {
        //
        // 摘要:
        //     使用当前用户存储区的个人证书初始化 System.Security.Cryptography.X509Certificates.X509Store 类的新实例。
        public X509Store();
// // 摘要: // 使用指定的存储区名称初始化 System.Security.Cryptography.X509Certificates.X509Store 类的新实例。 // // 参数: // storeName: // 一个表示存储区名称的字符串值。 有关更多信息,请参见 System.Security.Cryptography.X509Certificates.StoreName。 public X509Store(string storeName);
// // 摘要: // 使用指定的 System.Security.Cryptography.X509Certificates.StoreName 值初始化 System.Security.Cryptography.X509Certificates.X509Store // 类的新实例。 // // 参数: // storeName: // 指定 X.509 证书存储区名称的枚举值之一。 public X509Store(StoreName storeName);
// // 摘要: // 使用指定的 System.Security.Cryptography.X509Certificates.StoreLocation 值初始化 System.Security.Cryptography.X509Certificates.X509Store // 类的新实例。 // // 参数: // storeLocation: // 指定 X.509 证书存储区位置的枚举值之一。 public X509Store(StoreLocation storeLocation);
// // 摘要: // 使用 HCERTSTORE 存储区的 Intptr 句柄初始化 System.Security.Cryptography.X509Certificates.X509Store // 类的新实例。 // // 参数: // storeHandle: // HCERTSTORE 存储区的句柄。 // // 异常: // T:System.ArgumentNullException: // storeHandle 参数为 null。 // // T:System.Security.Cryptography.CryptographicException: // storeHandle 参数指向了无效的上下文。 public X509Store(IntPtr storeHandle);
// // 摘要: // 使用指定的 System.Security.Cryptography.X509Certificates.StoreName 和 System.Security.Cryptography.X509Certificates.StoreLocation // 值初始化 System.Security.Cryptography.X509Certificates.X509Store 类的新实例。 // // 参数: // storeName: // 指定 X.509 证书存储区名称的枚举值之一。 // // storeLocation: // 指定 X.509 证书存储区位置的枚举值之一。 // // 异常: // T:System.ArgumentException: // storeLocation 不是有效位置或者 storeName 不是有效名称。 public X509Store(StoreName storeName, StoreLocation storeLocation);
// // 摘要: // 使用一个字符串初始化 System.Security.Cryptography.X509Certificates.X509Store 类的新实例,该字符串表示 // System.Security.Cryptography.X509Certificates.StoreName 枚举中的一个值和 System.Security.Cryptography.X509Certificates.StoreLocation // 枚举中的一个值。 // // 参数: // storeName: // 一个字符串,表示 System.Security.Cryptography.X509Certificates.StoreName 枚举中的值。 // // storeLocation: // 指定 X.509 证书存储区位置的枚举值之一。 // // 异常: // T:System.ArgumentException: // storeLocation 包含无效值。 public X509Store(string storeName, StoreLocation storeLocation); // // 摘要: // 返回位于 X.509 证书存储区中的证书集合。 // // 返回结果: // 证书集合。 public X509Certificate2Collection Certificates { get; }
// // 摘要: // 获取 X.509 证书存储区的位置。 // // 返回结果: // 证书存储区的位置。 public StoreLocation Location { get; }
// // 摘要: // 获取 X.509 证书存储区的名称。 // // 返回结果: // 证书存储区的名称。 public string Name { get; }
// // 摘要: // 获取 HCERTSTORE 存储区的 System.IntPtr 句柄。 // // 返回结果: // HCERTSTORE 存储区的句柄。 // // 异常: // T:System.Security.Cryptography.CryptographicException: // 存储区未打开。 public IntPtr StoreHandle { get; }
// // 摘要: // 将证书添加到 X.509 证书存储区。 // // 参数: // certificate: // 要添加的证书。 // // 异常: // T:System.ArgumentNullException: // certificate 为 null。 // // T:System.Security.Cryptography.CryptographicException: // 未能将证书添加到存储区。 public void Add(X509Certificate2 certificate);
// // 摘要: // 将证书集合添加到 X.509 证书存储区。 // // 参数: // certificates: // 要添加的证书集合。 // // 异常: // T:System.ArgumentNullException: // certificates 为 null。 // // T:System.Security.SecurityException: // 调用方没有所要求的权限。 public void AddRange(X509Certificate2Collection certificates);
// // 摘要: // 关闭 X.509 证书存储区。 public void Close();
// // 摘要: // 打开 X.509 证书存储区或创建新存储区,具体取决于 System.Security.Cryptography.X509Certificates.OpenFlags // 标志设置。 // // 参数: // flags: // 枚举值的按位组合,指定打开 X.509 证书存储区的方式。 // // 异常: // T:System.Security.Cryptography.CryptographicException: // 该证书不可读。 // // T:System.Security.SecurityException: // 调用方没有所要求的权限。 // // T:System.ArgumentException: // 存储区包含无效值。 public void Open(OpenFlags flags);
// // 摘要: // 从 X.509 证书存储区移除证书。 // // 参数: // certificate: // 要移除的证书。 // // 异常: // T:System.ArgumentNullException: // certificate 为 null。 // // T:System.Security.SecurityException: // 调用方没有所要求的权限。 public void Remove(X509Certificate2 certificate);
// // 摘要: // 从 X.509 证书存储区移除一系列证书。 // // 参数: // certificates: // 要移除的一系列证书。 // // 异常: // T:System.ArgumentNullException: // certificates 为 null。 // // T:System.Security.SecurityException: // 调用方没有所要求的权限。 public void RemoveRange(X509Certificate2Collection certificates); }

  

2、StoreName:

   //
    // 摘要:
    //     指定要打开的 X.509 证书存储区的名称。
    public enum StoreName
    {
        //
        // 摘要:
        //     其他用户的 X.509 证书存储区。
        AddressBook = 1,
        //
        // 摘要:
        //     第三方证书颁发机构 (CA) 的 X.509 证书存储区。
        AuthRoot = 2,
        //
        // 摘要:
        //     中间证书颁发机构 (CA) 的 X.509 证书存储区。
        CertificateAuthority = 3,
        //
        // 摘要:
        //     吊销的证书的 X.509 证书存储区。
        Disallowed = 4,
        //
        // 摘要:
        //     个人证书的 X.509 证书存储区。
        My = 5,
        //
        // 摘要:
        //     受信任的根证书颁发机构 (CA) 的 X.509 证书存储区。
        Root = 6,
        //
        // 摘要:
        //     直接受信任的人和资源的 X.509 证书存储区。
        TrustedPeople = 7,
        //
        // 摘要:
        //     直接受信任的发行者的 X.509 证书存储区。
        TrustedPublisher = 8
    }

 

3、StoreLocation:

    //
    // 摘要:
    //     指定 X.509 证书存储区的位置。
    public enum StoreLocation
    {
        //
        // 摘要:
        //     当前用户使用的 X.509 证书存储区。
        CurrentUser = 1,
        //
        // 摘要:
        //     分配给本地计算机的 X.509 证书存储区。
        LocalMachine = 2
    }

 

4、OpenFlags

    //
    // 摘要:
    //     指定打开 X.509 证书存储区的方式。
    [Flags]
    public enum OpenFlags
    {
        //
        // 摘要:
        //     以只读方式打开 X.509 证书存储区。
        ReadOnly = 0,
// // 摘要: // 以读写方式打开 X.509 证书存储区。 ReadWrite = 1,
// // 摘要: // 以允许最高级访问的方式打开 X.509 证书存储区。 MaxAllowed = 2,
// // 摘要: // 仅打开现有存储区。如果不存在任何存储区,System.Security.Cryptography.X509Certificates.X509Store.Open(System.Security.Cryptography.X509Certificates.OpenFlags) // 方法不会创建新的存储区。 OpenExistingOnly = 4,
// // 摘要: // 打开 X.509 证书存储区并添加存档证书。 IncludeArchived = 8 }

 

5、public X509Certificate2Collection Find(X509FindType findType, object findValue, bool validOnly);

        //
        // 摘要:
        //     使用 System.Security.Cryptography.X509Certificates.X509FindType 枚举和 findValue 对象所指定的搜索条件搜索
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection 对象。
        //
        // 参数:
        //   findType:
        //     System.Security.Cryptography.X509Certificates.X509FindType 值之一。
        //
        //   findValue:
        //     作为对象的搜索条件。
        //
        //   validOnly:
        //     如果仅允许从搜索中返回有效的证书,则为 true;否则为 false。
        //
        // 返回结果:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection 对象。
        //
        // 异常:
        //   T:System.Security.Cryptography.CryptographicException:
        //     findType 无效。
        public X509Certificate2Collection Find(X509FindType findType, object findValue, bool validOnly);

  

 

6、X509FindType

    //
    // 摘要:
    //     指定通过 System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
    //     方法搜索的值的类型。
    public enum X509FindType
    {
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书的指纹。
        FindByThumbprint = 0,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书的主题名称。 与 System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectDistinguishedName
        //     枚举值所提供的相比,这是一种不太具体的搜索方式。 通过使用 System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName
        //     值,System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法将使用提供的值执行不区分大小写的字符串比较。 例如,如果将“MyCert”传递给 System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法,该方法将查找主题名称中包含该字符串的所有证书,而不考虑其他主题值。 按可分辨名称搜索的精确性较高。
        FindBySubjectName = 1,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书的主题可分辨名称。 与 System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName
        //     枚举值所提供的相比,这是一种更具体的搜索方式。 通过使用 System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectDistinguishedName
        //     值,System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法将对整个可分辨名称执行不区分大小写的字符串比较。 按主题名称搜索的精确性较低。
        FindBySubjectDistinguishedName = 2,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书颁发者的名称。 与 System.Security.Cryptography.X509Certificates.X509FindType.FindByIssuerDistinguishedName
        //     枚举值所提供的相比,这是一种不太具体的搜索方式。 通过使用 System.Security.Cryptography.X509Certificates.X509FindType.FindByIssuerName
        //     值,System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法将使用提供的值执行不区分大小写的字符串比较。 例如,如果将“MyCA”传递给 System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法,该方法将查找颁发者名称中包含该字符串的所有证书,而不管其是否包含其他颁发者值。
        FindByIssuerName = 3,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书的颁发者可分辨名称。 与 System.Security.Cryptography.X509Certificates.X509FindType.FindByIssuerName
        //     枚举值所提供的相比,这是一种更具体的搜索方式。 通过使用 System.Security.Cryptography.X509Certificates.X509FindType.FindByIssuerDistinguishedName
        //     值,System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法将对整个可分辨名称执行不区分大小写的字符串比较。 按颁发者名称搜索的精确性较低。
        FindByIssuerDistinguishedName = 4,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,它表示通过证书对话框显示的或 System.Security.Cryptography.X509Certificates.X509Certificate.GetSerialNumberString
        //     方法返回的证书的序列号,但不包含空格。
        FindBySerialNumber = 5,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个以当地时间表示的 System.DateTime 值。 可使用 System.DateTime.Now 查找当前有效的所有证书。
        FindByTimeValid = 6,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个以当地时间表示的 System.DateTime 值。 值不必处于将来时间。 例如,通过从 System.DateTime.Now的
        //     System.Security.Cryptography.X509Certificates.X509FindType.FindByTimeValid 的
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     操作的结果中采用去年最后一天的 System.Security.Cryptography.X509Certificates.X509FindType.FindByTimeNotYetValid
        //     的 System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     操作的结果的交集,可以使用 System.Security.Cryptography.X509Certificates.X509FindType.FindByTimeNotYetValid
        //     来查找已在当前年中有效的证书。
        FindByTimeNotYetValid = 7,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个以当地时间表示的 System.DateTime 值。 例如,通过从 System.DateTime.Now 的
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     操作的结果中消除今年的最后一天的 System.Security.Cryptography.X509Certificates.X509FindType.FindByTimeExpired
        //     的 System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     操作的结果,可以查找有效期截止到今年年底的所有证书。
        FindByTimeExpired = 8,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书的模板名,如“ClientAuth”。 模板名为 X509 3 版扩展名,指定证书的使用。
        FindByTemplateName = 9,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书的应用程序策略友好名称或对象标识符(OID,或 System.Security.Cryptography.Oid)。
        //     例如,可以使用“加密文件系统”或“1.3.6.1.4.1.311.10.3.4”。 对于将要本地化的应用程序,由于友好名称经过本地化处理,因此必须使用 OID
        //     值。
        FindByApplicationPolicy = 10,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串表示证书策略的友好名称或对象标识符(OID,或 System.Security.Cryptography.Oid)。
        //     最佳做法是使用 OID,如“1.3.6.1.4.1.311.10.3.4”。 对于将要本地化的应用程序,由于友好名称经过本地化处理,因此必须使用 OID。
        FindByCertificatePolicy = 11,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串描述要查找的扩展名。 对象标识符 (OID) 常用于指示 System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法搜索扩展名与 OID 值相匹配的所有证书。
        FindByExtension = 12,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个表示密钥用法的字符串或一个表示位掩码(包含所有请求的密钥用法)的整数。 对于字符串值,一次只能指定一种密钥用法,但是可以按级联序列使用
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法以使请求用法交叉。 例如,可以将 findValue 参数设置为“KeyEncipherment”或整数(0x30 指示“KeyEncipherment”和“DataEncipherment”)。
        //     还可以使用 System.Security.Cryptography.X509Certificates.X509KeyUsageFlags 枚举的值。
        FindByKeyUsage = 13,
        //
        // 摘要:
        //     System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Find(System.Security.Cryptography.X509Certificates.X509FindType,System.Object,System.Boolean)
        //     方法的 findValue 参数必须是一个字符串,该字符串用十六进制值表示主题密钥标识符,如 UI 中显示的“F3E815D45E83B8477B9284113C64EF208E897112”。
        FindBySubjectKeyIdentifier = 14
    }

  

 

posted @ 2016-12-03 15:34  Kevin.W  阅读(3652)  评论(0编辑  收藏  举报