/// <summary> /// Retrieves a named certificate. /// </summary> /// <param name="name">The name.</param> /// <param name="password">The password.</param> /// <returns>A X509Certificate2</returns> public static X509Certificate2 GetCertificate(string name, string password) { Contract.Requires(!String.IsNullOrEmpty(name)); Contract.Requires(!String.IsNullOrEmpty(password)); Contract.Ensures(Contract.Result <X509Certificate2>() != null); CertificateReferenceElement certref = GetReference(name); bool isHosted = HostingEnvironment.IsHosted; if (!string.IsNullOrEmpty(certref.Filename)) { string filename = certref.Filename; if (isHosted) { filename = string.Format("{0}\\{1}\\{2}", HostingEnvironment.ApplicationPhysicalPath, "App_Data\\certificates", certref.Filename); } if (!string.IsNullOrEmpty(password)) { return(new X509Certificate2(filename, password)); } else { return(new X509Certificate2(filename)); } } try { return(GetCertificateFromStore( certref.StoreLocation, certref.StoreName, certref.X509FindType, certref.FindValue)); } catch { throw new ConfigurationErrorsException(String.Format("Certificate for section {0} not found", name)); } }
/// <summary> /// Gets the element key for a specified configuration element when overridden in a derived class. /// </summary> /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param> /// <returns> /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>. /// </returns> protected override object GetElementKey(ConfigurationElement element) { CertificateReferenceElement reference = (CertificateReferenceElement)element; return(reference.Name); }