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();
}