IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

ajouter automatiquement un certificat sous un compact frameWork


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    211
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 211
    Par défaut ajouter automatiquement un certificat sous un compact frameWork
    Bonjour tout le monde,

    Je galère ça fait deux semaine pour l'ajout d'un certificat client dans mon Windows CE , je m'explique: j'ai un certificat client que je dois ajouter automatiquement sous internet explorer afin de pourvoir ouvrir un https, travaillant sou c# j'ai essayé cetet fonction (qui ne marche pas bien évidement):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
            public static void ImportCert(string Path)
            {
                //Ouverture du magazin 'MY'
                X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
                store.Open(OpenFlags.ReadWrite);
     
                X509CertificateCollection myCollection = store.Certificates;
     
                //convertir le certificat en tableau de Bytes             
                byte[] certByte = GetFile(Path);
     
                //Création de l'objet x509 à partir du tableau de bytes
                X509Certificate x509 = new X509Certificate(certByte);
     
                //Ajout du certificat à la collection du magasin 'MY'
                myCollection.Add(x509);
     
                IntPtr cert_MY_store = CertOpenStore(new IntPtr(CERT_STORE_PROV_SYSTEM_W),
                                                0,
                                                IntPtr.Zero,
                                                CERT_SYSTEM_STORE_CURRENT_USER,
                                                "MY");
                IntPtr hCertCntxt = IntPtr.Zero;
     
                uint b = CertCreateCertificateContext(X509_ASN_ENCODING, certByte, (uint)certByte.Length);
                int i = CertAddCertificateContextToStore(cert_MY_store, hCertCntxt, (int)CERT_STORE_ADD_REPLACE_EXISTING, IntPtr.Zero);
                //ajouter au registre
                Microsoft.Win32.RegistryKey oReg = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Comm\\Security\\SystemCertificates\\my\\Certificates\\6B314720F126EFE681EF72EACFCE477E83D3B9D0");
                oReg.SetValue("Blob", certByte);
            }
    Est ce qu'il y a quelqu'un qui pourra m'aider???

    Amicalement

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    211
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 211
    Par défaut
    J'ai une piste: le code suivant permet d'importer des certificats .DER

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     
            public static void ImportCert(string Path)
            {
                ////convertir le certificat en tableau de Bytes             
                byte[] certByte = GetFile(Path););
     
                IntPtr cert_MY_store = CertOpenStore(new IntPtr(CERT_STORE_PROV_SYSTEM),
                                                0,
                                                IntPtr.Zero,
                                                CERT_SYSTEM_STORE_CURRENT_USER,
                                                "MY");
                IntPtr hCertCntxt = IntPtr.Zero;
     
                hCertCntxt = CertCreateCertificateContext(X509_ASN_ENCODING, certByte, (uint)certByte.Length);
     
                int i = CertAddCertificateContextToStore(cert_MY_store, hCertCntxt, (int)CERT_STORE_ADD_REPLACE_EXISTING, IntPtr.Zero);
     
     
                CertCloseStore(cert_MY_store, 0);
            }
    ça marche nikel suaf que dans mon cas j'ai besoin d'un certificat client en .p12 j'ai vu qu'il y a une fonction sous la dll advapi32.dll qui s'appelle CryptImportKey mais j'ai pas encore réussi a l'intégrer.

    Voili voilou, Si quelqu'un a une piste je suis prôneuse.

    Amicalement

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    211
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 211
    Par défaut problème résolu YOUPIIII
    Bonjour tt le monde,

    Après tant de recherche voici la solution pour ceux qui sont intéressés :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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;
                }
     
            }
    voili voilou

    Amicalement

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2007] Ajout automatique vers un sous formulaire
    Par Gorane dans le forum IHM
    Réponses: 2
    Dernier message: 13/11/2010, 16h59
  2. Réponses: 0
    Dernier message: 06/07/2010, 18h18
  3. Application en arriere plan sous compact framework
    Par ouadie99 dans le forum Windows Mobile
    Réponses: 3
    Dernier message: 09/04/2010, 14h38
  4. Compact Framework .NET sous Delphi
    Par Laurent Dardenne dans le forum Delphi .NET
    Réponses: 3
    Dernier message: 29/05/2008, 18h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo