web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Getting a security certificate from crm plugin

(0) ShareShare
ReportReport
Posted on by

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

*This post is locked for comments

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans