Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Getting a security certificate from crm plugin

$
0
0

Hello,

I am trying to retrieve from the local machine store a security certificate with a private key in order to connect to a web service and send some data.

I have installed the certificate in the personal and root stores.

In the personal store i have given access to the certificate private key to the network service user(under which crm is running in IIS), Everyone and the user i am using to log on to CRM.

Here is the code:

private X509Certificate2 GetCert(string thumbprint = "46 7f 86 4c 9f c7 d8 24 a8 19 fd f6 6a 12 9f 58 f4 fc 58 56")
        {
            byte[] thumbprintArray = new byte[thumbprint.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length];
            string[] stringArray = thumbprint.Split(new char[] { ' ' });
            
            for (int i = 0; i < thumbprintArray.Length; i++)
            {
                string s = ((string)(stringArray[i])).Trim();
                thumbprintArray[i] = Convert.ToByte(s, 16);
            }

            //X509Store localStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
          
            X509Store localStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            localStore.Open(OpenFlags.MaxAllowed);
            
            X509Certificate2Collection certCol = localStore.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
            if (certCol.Capacity == 0)
                throw new Exception("No certificates!");
            
            X509Chain chain = new X509Chain();
            chain.ChainPolicy = new X509ChainPolicy()
            {
                VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority | X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown,
                RevocationMode = X509RevocationMode.NoCheck
            };
            
            string allCert = string.Empty;
            foreach (X509Certificate2 cert in certCol)
            {
                Entity qm = new Entity("new_lmtinterfacelog");
                qm["new_name"] = "Certificate not found!";
                qm["new_errormessage"] = cert.Thumbprint;
                service.Create(qm);
                allCert = string.Concat(allCert, " // " ,cert.GetCertHashString().ToUpper());
                if (cert.GetCertHashString().ToUpper() == thumbprint.Replace(" ", "").ToUpper())
                {
                    Entity qm1 = new Entity("new_lmtinterfacelog");
                    qm1["new_name"] = "Certificate found!";
                    qm1["new_errormessage"] = cert.GetCertHashString().ToUpper();
                    service.Create(qm1);
                    try
                    {
                        bool certOK = cert.Verify();

                    }
                    catch (CryptographicException cEx)
                    {
                        throw cEx;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    return cert;
                }
            }
            
            //throw new Exception(allCert);
            return null;

        }

This method is returning a lot of certificate just not the one i need...

If i run this code in  a simple console application i can see the certificate.

How do i get the certificate from the CRM plugin????

Tnx


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>