C# 如何查看IIS上的站点是否使用https证书运行
假设我有两台服务器。一个运行https证书,另一个不运行。 访问我的某个站点的用户键入http。如果该站点指向具有https证书的服务器,那么我想重定向用户/重写url 只剩下一件事了。如何检查我的站点是否获得证书?或者如果绑定是httpsC# 如何查看IIS上的站点是否使用https证书运行,c#,iis,ssl,https,C#,Iis,Ssl,Https,假设我有两台服务器。一个运行https证书,另一个不运行。 访问我的某个站点的用户键入http。如果该站点指向具有https证书的服务器,那么我想重定向用户/重写url 只剩下一件事了。如何检查我的站点是否获得证书?或者如果绑定是https void Application_BeginRequest(object sender, EventArgs e) { var path = HttpContext.Current.Request.Url.AbsolutePath; var
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://"));
}
}
未经测试,但应提供一些关于如何进行测试的见解
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);
}