static bool CheckThatCertificateWasLoadedWithPrivateKey(X509Certificate2 certificate) { try { if (!HasPrivateKey(certificate)) { var message = new StringBuilder(); message.AppendFormat("The X509 certificate {0} was loaded but the private key was not loaded.", certificate.Subject).AppendLine(); try { string privateKeyPath = CryptUtils.GetKeyFilePath(certificate); message.AppendLine("The private key file should be located at: " + privateKeyPath); if (!File.Exists(privateKeyPath)) { message.AppendLine("However, the current user does not appear to be able to access the private key file, or it does not exist."); } message.AppendLine("Attempting to grant the user " + CrossPlatform.GetUserDomainName() + "\\" + CrossPlatform.GetUserName() + " access to the certificate private key directory."); try { GrantCurrentUserAccessToPrivateKeyDirectory(privateKeyPath); message.AppendLine("The user should now have read access to the private key. The certificate will be reloaded."); } catch (Exception ex) { message.AppendLine("Unable to grant the current user read access to the private key: " + ex.Message); } } catch (Exception ex) { message.AppendLine("Furthermore, the private key file could not be located: " + ex.Message); } Log.Warn(message.ToString().Trim()); return(false); } return(true); } catch (Exception ex) { Log.Warn(ex.ToString()); return(false); } }