说我有两台服务器。一个运行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://"));
}
}
答案 0 :(得分:0)
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);
}
没有经过测试,但应该对如何做到这一点有所了解。