Thursday, April 16, 2009

Forcing pages to use SSL through ASP.Net

Save the below code (choose any one according to your applicaiton's language code) to the root of your application into a file named "ForceSSL.inc" using a text editor.

For VB.Net application
===================
<% If Request.ServerVariables("SERVER_PORT")=80 Then
Dim strSecureURL
strSecureURL = "https://"
strSecureURL = strSecureURL & Request.ServerVariables("SERVER_NAME")
strSecureURL = strSecureURL & Request.ServerVariables("URL")
Response.Redirect strSecureURL
End If %>

For C#.Net application
=====================
<% if(Request.ServerVariables["SERVER_PORT"]=="80")
{
string strSecureURL="";
strSecureURL = "https://";
strSecureURL = strSecureURL + Request.ServerVariables["SERVER_NAME"]; strSecureURL = strSecureURL + Request.ServerVariables["URL"];
Response.Redirect(strSecureURL);
}
%>

For each .aspx page that requires SSL, paste the following code at the top of the page (or under content's place holder if you are using master pages) to reference the include file from the previous step:

<!--#include virtual="ForceSSL.inc"-->

No comments:

Post a Comment