Digital Colony!

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)

Labels:

posted by MAS on Jan 8,2007

Legacy Comments

If you found this site helpful, consider buying me a cup of coffee. Smile

Digital Colony Copyright © 1999-2010 XHTML   508
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.