1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
/// <summary>
/// Importer un certificat client
/// </summary>
/// <param name="certificatePath"></param>
/// <param name="PassWord"></param>
/// <returns></returns>
public static bool ImportCertificate(string certificatePath, string PassWord)
{
IntPtr pctx = IntPtr.Zero;
try
{
byte[] Data = GetFile(certificatePath);
//Création du Blob
CRYPT_DATA_BLOB blob = new CRYPT_DATA_BLOB();
blob.cbData = Data.Length;
blob.pbData = Marshal.AllocHGlobal(Data.Length);
//Copier le contenu du DATA dans le BLOB
Marshal.Copy(Data, 0, blob.pbData, Data.Length);
IntPtr PFXImport = PFXImportCertStore(ref blob, PassWord, CRYPT_EXPORTABLE);
//if (PFXImport == IntPtr.Zero)
//{
// int i = Marshal.GetLastWin32Error();
//}
//Ouvrir le magasin "MY"
IntPtr myStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, IntPtr.Zero, CERT_SYSTEM_STORE_CURRENT_USER, "MY");
//Enumération du store
while ((pctx = CertEnumCertificatesInStore(PFXImport, pctx)) != IntPtr.Zero)
{
int i = CertAddCertificateContextToStore(myStore, pctx, (int)CERT_STORE_ADD_NEW, IntPtr.Zero);
}
CertCloseStore(myStore, 0);
return true;
}
catch(Exception err)
{
MessageBox.Show(err.Message);
return false;
}
} |
Partager