【问题标题】:How to see if site on IIS running with https certificate如何查看 IIS 上的站点是否使用 https 证书运行
【发布时间】:2014-06-17 16:10:15
【问题描述】:

假设我有两台服务器。一个使用 https 证书运行,而另一个则没有。 访问我的网站之一的用户键入 http。如果该站点指向具有 https 证书的服务器,那么我想重定向用户/重写 url。

只剩下一件事了。如何检查我的网站是否获得了证书?或者如果绑定是https?

void Application_BeginRequest(object sender, EventArgs e)
{
    var path = HttpContext.Current.Request.Url.AbsolutePath;
    var variableToSeeIfIISSiteRunningOnCertificate = true;
    if (variableToSeeIfIISSiteRunningOnCertificate && !HttpContext.Current.Request.IsSecureConnection && !string.IsNullOrEmpty(path) && path.ToLower().Contains("loginpage.aspx"))
    {
        HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("http://", "https://"));
    }
}

【问题讨论】:

    标签: c# iis ssl https


    【解决方案1】:
    using System.Security;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    
    public bool HasValidCertificate(url){
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Close();
        X509Certificate cert = request.ServicePoint.Certificate;
        X509Certificate2 cert2 = new X509Certificate2(cert);
        string cn = cert2.GetIssuerName();
        string cedate = cert2.GetExpirationDateString();
        string cpub = cert2.GetPublicKeyString();
    
        return !string.IsNullOrEmpty(cn) && cedate > DateTime.Now && !string.IsNullOrEmpty(cpub);
    }
    

    未经测试,但应该提供一些关于如何做的见解。

    【讨论】:

      猜你喜欢
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2016-06-07
      • 2011-09-01
      • 2012-12-01
      • 2017-10-06
      相关资源
      最近更新 更多