public void LoadCliCertFromCerStore(string certStore, string subjectName, bool isMachineStore) { try { CAPICOM.Store store = new CAPICOM.Store(); // store.Open(isMachineStore?CAPICOM.CAPICOM_STORE_LOCATION.CAPICOM_LOCAL_MACHINE_STORE:CAPICOM.CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, // certStore, CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY); store.Open(CAPICOM.CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, certStore, CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY); foreach (CAPICOM.Certificate cert in store.Certificates) { if (cert.SubjectName.IndexOf(subjectName) > 0) { ClientCert = cert; return; } } } catch (COMException e) { throw e; } }
CAPICOM.ICertificate2 GetCertificateByThumbprint(string certThumbprint) { CAPICOM.ICertificate2 ret = null; string test = ""; try { CAPICOM.Store oStore = new CAPICOM.Store(); oStore.Open(CAPICOM.CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, OpenMode: CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_MAXIMUM_ALLOWED); foreach (CAPICOM.ICertificate2 certif in oStore.Certificates) { test += certif.Thumbprint + " "; if (certif.Thumbprint == certThumbprint.ToUpper()) { ret = certif; break; } } } catch (Exception e) { new Exception(e.Message + " " + e.StackTrace); } if (ret == null) { throw new Exception("Пустой сертификат " + test); } return(ret); }