Forcing a page to use a secure connection

Posted by Abhisek under ASP.NET category on | Views : 2425
A page where the user enter sensitive data should be kept secure. we can do it using the https protocol instead of http. If the request for the page using secure connection is made from a insecure page, we can force the page to use a secure connection.
Just add the following code in your web page's page load event.

 protected void Page_Load(object sender, EventArgs e)
if (Request.!IsSecureConnection)
string url = Request.Url.ToString().Replace("https:", "http:");

We are checking whether the request is secure by using the IsSecureConnection property. It is true when current connection is secure otherwise false.

Here we are replacing the http protocol with https and passing the URL of the current page and then redirecting the browser to the new URL.

Comments or Responses

Login to post response