Exemple #1
0
        /// <summary>
        /// Finds the X509 certificate in this machine's key store.
        /// </summary>
        /// <param name="friendlyName">
        /// Friendly name of the certificate
        /// </param>
        /// <returns>
        /// X509Certificate2 object that matches the given friendly name.
        /// </returns>
        public static X509Certificate2 GetCertificateByFriendlyName(string friendlyName)
        {
            X509Certificate2 cert         = null;
            X509Store        store        = new X509Store(StoreLocation.LocalMachine);
            string           errorMessage = null;

            try
            {
                store.Open(OpenFlags.ReadOnly);

                StringBuilder logMessageCert = new StringBuilder();
                logMessageCert.Append("GetCertificateByFriendlyName(): looking for: \"").Append(friendlyName).Append("\" certificate\r\n");
                FedletLogger.Info(logMessageCert.ToString());

                X509Certificate2Enumerator certEnum = store.Certificates.GetEnumerator();
                while (certEnum.MoveNext())
                {
                    logMessageCert.Clear();
                    logMessageCert.Append("GetCertificateByFriendlyName(): found: \"").Append(certEnum.Current.FriendlyName).Append("\" certificate\r\n");
                    FedletLogger.Info(logMessageCert.ToString());
                    if (certEnum.Current.FriendlyName == friendlyName)
                    {
                        cert = certEnum.Current;
                        break;
                    }
                }
            }
            catch (CryptographicException ce)
            {
                errorMessage = ce.Message;
            }
            catch (SecurityException se)
            {
                errorMessage = se.Message;
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }

            if (errorMessage != null)
            {
                FedletLogger.Warning(Resources.FedletCertificateFactoryGetByFriendlyNameFailed + " " + errorMessage);
            }

            return(cert);
        }
        /// <summary>
        /// Finds the X509 certificate in this machine's key store.
        /// </summary>
        /// <param name="friendlyName">
        /// Friendly name of the certificate
        /// </param>
        /// <returns>
        /// X509Certificate2 object that matches the given friendly name.
        /// </returns>
        public static X509Certificate2 GetCertificateByFriendlyName(string friendlyName)
        {
            X509Certificate2 cert         = null;
            X509Store        store        = new X509Store(StoreLocation.LocalMachine);
            string           errorMessage = null;

            try
            {
                store.Open(OpenFlags.ReadOnly);

                X509Certificate2Enumerator certEnum = store.Certificates.GetEnumerator();
                while (certEnum.MoveNext())
                {
                    if (certEnum.Current.FriendlyName == friendlyName)
                    {
                        cert = certEnum.Current;
                        break;
                    }
                }
            }
            catch (CryptographicException ce)
            {
                errorMessage = ce.Message;
            }
            catch (SecurityException se)
            {
                errorMessage = se.Message;
            }
            finally
            {
                if (store != null)
                {
                    store.Close();
                }
            }

            if (errorMessage != null)
            {
                FedletLogger.Warning(Resources.FedletCertificateFactoryGetByFriendlyNameFailed + " " + errorMessage);
            }

            return(cert);
        }
Exemple #3
0
 /// <summary>
 /// Method to write an information message to the event log.
 /// </summary>
 /// <param name="message">Message to be written.</param>
 public static void Info(string message)
 {
     FedletLogger.LogMessage(message, EventLogEntryType.Information);
 }
Exemple #4
0
 /// <summary>
 /// Method to write an error message to the event log.
 /// </summary>
 /// <param name="message">Message to be written.</param>
 public static void Error(string message)
 {
     FedletLogger.LogMessage(message, EventLogEntryType.Error);
 }
Exemple #5
0
 /// <summary>
 /// Method to write a warning message to the event log.
 /// </summary>
 /// <param name="message">Message to be written.</param>
 public static void Warning(string message)
 {
     FedletLogger.LogMessage(message, EventLogEntryType.Warning);
 }