About The Author
Michael Flynn is a Web Developer at Terralever, an interactive marketing agency based in Tempe, AZ. He specializes in web technologies that include C# .NET, SQL, XML, AJAX, jQuery, Flash, and also skills in Photoshop and Illustrator. He was been involved in web development since 1998, and earned a Bachelors and Masters degree in Computer Engineering and Computer Science from the Univerisity of Louisville and holds an MSCT certificate in Web Applications.
Calendar
<<  September 2010  >>
SMTWTFS
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Http To Https And Back with a Basepage in ASP.NET C#

I needed to have one page redirect to a https protocol, and when leaving that page go back to the http protocol.  I wanted a easy way that would be handled in one place, but have the ability to be overridden when necessary.  I always create a basepage for all my pages, so I decided to create a method that a page can override.  I have a page called checkout.aspx that needs to have https, so I override the parent class to redirect it to the https page.  If the user leaves the page, they will then be redirected back to http with the basepage implementation.

Basepage.cs

public virtual void SetProtocol()

{

    if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "on")

    {

        string url = string.Format("http://{0}{1}", HttpContext.Current.Request.Url.Authority, Request.RawUrl);

        Response.Redirect(url, true);

    }

}

 

protected override void OnInit(EventArgs e)

{

    SetProtocol();

    base.OnInit(e);

}

 

 

 

Checkout.cs

 

public override void SetProtocol()

{

    if (Request.ServerVariables["HTTPS"].ToString().ToLower() == "off")

    {

        string url = string.Format("{0}/checkout", Utility.RootUrl).Insert(4, "s");

        Response.Redirect(url, true);

    }

}

 

protected void Page_Load(object sender, EventArgs e)

{

    SetProtocol();

}

Posted on 9/25/2008 11:05:00 PM by cblaze22

Permalink | Comments (1) | Post RSSRSS comment feed |

Categories: ASP.NET | Development Tips | Tips

Tags:

Be the first to rate this post

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


Comments

May 8. 2010 01:43

payday loans advance

Well-written blog post. I have bookmarked it already. Have a nice day.

payday loans advance us

Add A Comment




  Country flag