मुझे लगता है कि कैमिलो के उत्तर में एक छोटी सी समस्या है। जहां तक मैं देख सकता हूं (परीक्षण किया गया) प्रमाण पत्र खोजने के लिए कोड काम नहीं करता है, क्योंकि System.Convert.ToBase64String(binding.CertificateHash)
एक वैध प्रमाणपत्र थंबप्रिंट वापस नहीं करता है।
मेरे संस्करण:
/// <summary>
/// Returns the https certificate used for a given local IIS website.
/// </summary>
/// <param name="sWebsite">Website url, e.g., "https://myserver.company.com"</param>
/// <returns>certificate, null if not found</returns>
private X509Certificate2 FindIisHttpsCert(string sWebsite)
{
Uri uriWebsite = new Uri(sWebsite);
using (ServerManager sm = new ServerManager())
{
string sBindingPort = string.Format(":{0}:", uriWebsite.Port);
Binding bdBestMatch = null;
foreach (Site s in sm.Sites)
{
foreach (Binding bd in s.Bindings)
{
if (bd.BindingInformation.IndexOf(sBindingPort) >= 0)
{
string sBindingHostInfo = bd.BindingInformation.Substring(bd.BindingInformation.LastIndexOf(':') + 1);
if (uriWebsite.Host.IndexOf(sBindingHostInfo, StringComparison.InvariantCultureIgnoreCase) == 0)
{
if ((bd.Protocol == "https") && ((bdBestMatch == null) || (bdBestMatch.BindingInformation.Length < bd.BindingInformation.Length)))
bdBestMatch = bd;
}
}
}
}
if (bdBestMatch != null)
{
StringBuilder sbThumbPrint = new StringBuilder();
for (int i = 0; i < bdBestMatch.CertificateHash.Length; i++)
sbThumbPrint.AppendFormat("{0:X2}", bdBestMatch.CertificateHash[i]);
X509Store store = new X509Store(bdBestMatch.CertificateStoreName, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection coll = store.Certificates.Find(X509FindType.FindByThumbprint, sbThumbPrint.ToString(), true);
if (coll.Count > 0)
return coll[0];
}
}
return null; // if no matching site was found
}
यह समारोह भी काम करता है यदि एक से अधिक https साइटों एक ही सर्वर पर होस्ट कर रहे हैं (परीक्षण किया) और साइट से 443 (परीक्षण नहीं) अन्य एक बंदरगाह का उपयोग करता है, तो काम करना चाहिए। बाध्यकारी जानकारी प्राप्त करने के लिए, %windir%\system32\inetsrv\Microsoft.Web.Administration.dll
का उपयोग कैमिलो के उत्तर में किया जाता है।
स्रोत
2016-09-06 15:43:50
कि NuGet पैकेज माइक्रोसॉफ्ट आधिकारिक नहीं है और इसे टालना चाहिए। –
वह फ़ील्ड किसी को भी सेट किया जा सकता है। प्रकाशक की जांच करें और आप देखेंगे। यह असेंबली हमेशा आईआईएस स्थापना फ़ोल्डर से जोड़ा जाना चाहिए। –
पूरा पथ '% windir% \ system32 \ inetsrv \ Microsoft.Web.Administration.dll' है। कृपया इसका प्रयोग करें। –