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 }