How to easily redirect from http to https in ASP.NET

by APIJunkie 2/23/2008 9:58:00 AM

Below is a simple code snippet to redirect from a non secure HTTP URL to a secure HTTPS URL using C# and ASP.NET.

The code checks if we are using a secure connection and if not it redirects to the same requested URL changing the protocol from HTTP to HTTPS.

////////////////////////////////////////////////////////////////

// Check if we are using https and if not redirect

///////////////////////////////////////////////////////////////

if(!Request.IsSecureConnection)

{

 
string redirectUrl = Request.Url.ToString().Replace("http:", "https:");

  Response.Redirect(redirectUrl);

}

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET | How To | Web Development

Comments

2/23/2008 10:05:34 AM

trackback

Trackback from DotNetKicks.com

How to easily redirect from http to https in ASP.NET

DotNetKicks.com

3/27/2008 8:43:15 PM

fosiul

Hi, your code is good, but there is one problem
when i am going out from https page to another page which is actually http, its not going to http, its always staying in https for rest of the pages.

i understand, now i will have to write a code to go to https to http for other pages aswell. which would be problamatic, its not it ?

is not there any other way to write code for http to https redirection ?

fosiul gb

3/28/2008 12:53:37 AM

APIJunkie

Hi Fosiul,
Thank you for your comment.
I have compiled a new blog entry to try and further address the issue of easy transitions from HTTP to HTTPS and vice versa.

www.apijunkie.com/.../post.aspx

APIJunkie us

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

11/19/2008 7:35:56 PM

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author

My name is Bacon…James Bacon.

I am an API wars veteran I was wounded by x86 assembly, recovered and moved on to C. I am currently stuck in C++ and sniffing .NET.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

E-mail me Send mail


Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in