2014-06-17 3 views
0

두 개의 서버가 있다고 가정 해보십시오. 하나는 https 인증서로 실행하고 다른 하나는 실행하지 않습니다. 내 사이트 중 하나를 방문하는 사용자가 http를 입력합니다. https 인증서가있는 서버를 가리키는 사이트가 사용자를 리디렉션하고 URL을 다시 쓰려고합니다.https 인증서로 실행중인 IIS에서 사이트를 확인하는 방법

남아있는 것은 하나뿐입니다. 내 사이트에 인증서가 있는지 어떻게 확인합니까? 또는 바인딩이 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
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); 
} 

테스트하지만 작업을 수행하는 방법에 대한 몇 가지 통찰력을 제공해야하지.

관련 문제