From HTTP to HTTPS and back (continued discussion)

by APIJunkie 3/27/2008 11:58:00 PM

A couple of weeks ago I showed an easy way to redirect from HTTP to HTTPS.

Fosiul raised the issue that once you redirect to HTTPS all the pages become secure even the ones we do not need to be secure.

This can lead to some problems like adding additional stress on the web server machine.

To solve this problem we should be able to switch from https to http when ever we detect a page where we do not need secure communications.

One way to do that is to have a function that knows to switch both ways.

Example:

// set protocol to secure or unsecured according to bSecure flag.

// bSecure flag = true -> secure connection

// bSecure flag = false -> unsecured connection

public void setSecureProtocol(bool bSecure)

{

string redirectUrl = null;

// if we want HTTPS and it is currently HTTP

if (bSecure && !Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("http:", "https:");

else

// if we want HTTP and it is currently HTTPS

if (!bSecure && Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("https:", "http:");

//else

// in all other cases we don't need to redirect

// check if we need to redirect, and if so use redirectUrl to do the job

if(redirectUrl!=null)

Response.Redirect(redirectUrl);

}

One way to use the above function is in the page load handler of pages where a transition to or from HTTP or HTTPS should occur.

Putting the function call in the page load handler would make sure that a transition from secure to non secure and vice versa would occur before the rest of the page gets rendered.

Example:

protected void Page_Load(object sender, EventArgs e)

{

// set to HTTPS secure protocol

setSecureProtocol(true);

/// rest of code goes here

/// we will only reach this code in secure mode

}

If you want to be fancier you can use a custom HTTP module to detect and redirect using a variant of the setSecureProtocol function.

Example:

/// <summary>

/// HttpToHttpsRedirector - an http module to detect and redirect from http to https and vice versa

/// </summary>

public class HttpToHttpsRedirector: IHttpModule

{

 

public void Init(HttpApplication context)

{

context.BeginRequest +=
new System.EventHandler(Application_BeginRequest);

}

 

// your BeginRequest event handler.

private void Application_BeginRequest(Object source, EventArgs e)

{

HttpApplication application = (HttpApplication)source;

HttpContext context = application.Context;

// This is where your web site logic should decide if to redirect to http/https.

// The example code below always redirects to https!!!

if (true) setSecureProtocol(context, true);

}

public void Dispose()

{

}

// utility functions

// set protocol to secure or unsecure acording to bSecure flag.

// bSecure flag = true -> secure connection

// bSecure flag = false -> unsecure connection

public void setSecureProtocol(HttpContext context, bool bSecure)

{

string redirectUrl = null;

// if we want HTTPS and it is currently HTTP

if (bSecure && !context.Request.IsSecureConnection) redirectUrl = context.Request.Url.ToString().Replace("http:", "https:");

else

// if we want HTTP and it is currently HTTPS

if (!bSecure && context.Request.IsSecureConnection) redirectUrl = context.Request.Url.ToString().Replace("https:", "http:");

//else

// in all other cases we don't need to redirect

// check if we need to redirect, and if so use redirectUrl to do the job

if (redirectUrl != null)

context.Response.Redirect(redirectUrl);

}

}

Good luck!

Currently rated 5.0 by 2 people

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

Tags:

.NET | Web Development

Comments

3/28/2008 12:42:07 AM

trackback

Trackback from DotNetKicks.com

From HTTP to HTTPS and back (continued discussion)

DotNetKicks.com

3/28/2008 1:17:43 AM

fosiul

Hi, thanks for quick response and write another programm.
Actually i am new in asp.net, so will you please exlain me one thing please. here i am interested your custom http module solutions.

after creating HttpToHttpsRedirector class,i understand that, i will have to pass value which is setSecureProtocol(true) to this class, is that right ?

but how i will pass this value ?? i understand that i will have to call this HttpToHttpsRedirector class to everypage to pass this value, is this right ?? if yes, i will do that ?
Please advise.

Last thing : if i just copy code and past it to "HttpToHttpsRedirector.vb" will it work ?? or do i have to edit aswell ?

Thanks in advance

fosiul gb

3/28/2008 1:19:44 AM

fosiul

One more thing, can i get a code which is written by Vb.net ( i just noticed, you wrote code by C#)
it would realy help me.

fosiul gb

3/28/2008 3:55:48 AM

Chris

I have been using Sanibel Logic's SSLRedirect (www.sanibellogic.com/.../Products.aspx) with great success. Sure it cost a little bit of money ($40.00) but it's a clean implementation and supports regular expression matching on the query string. The latest version even supports IIS 7 Integrated Pipeline.

Have a look and see if this works for you...

Chris us

3/28/2008 10:09:37 AM

APIJunkie

Fosiul,
To learn more about creating custom HTTP modules you can check out the tutorial at http://msdn2.microsoft.com/en-us/library/ms227673(VS.80).aspx
To convert code from C# to VB.NET you can use an online code converter like the one at labs.developerfusion.co.uk/.../csharp-to-vb.aspx

APIJunkie us

3/28/2008 10:12:06 AM

APIJunkie

Thanks Chris,
I will have a look.

APIJunkie us

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

11/19/2008 8:23:14 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