Thread: ASP.NET/Detecting HTTP vs HTTPS in ASP.NET

Detecting HTTP vs HTTPS in ASP.NET

One simple command will let you know if the user is accessing your page via SSL.



Request.IsSecureConnection

Example


if (Request.IsSecureConnection)

{
    Response.Write("HTTPS");
}
else
{
    Response.Write("HTTP");
}

Redirecting to HTTPS


if (!Request.IsSecureConnection)

{
    // send user to SSL
    string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]);        
    string filePath = Request.FilePath;
    Response.Redirect("https://" + serverName + filePath);
}

HttpRequest.IsSecureConnection Property (MSDN)





Re: Detecting HTTP vs HTTPS in ASP.NET

Original:


digitalcolony.com/2007/01/detecting-http-vs-https-in-aspnet.aspx