On my master page I have the functions:
/// <summary>
/// Forces user to use unsecure HTTP connection
/// </summary>
public void FoceUnsecure()
{
SSLHTTPDirect(false);
}
/// <summary>
/// Forces user to redirect to SSL
/// </summary>
public void ForceSSL()
{
SSLHTTPDirect(true);
}
/// <summary>
/// Perform the redirect to self
/// </summary>
/// <param name="SSLRequired">True = Force HTTPS, False = Force HTTP</param>
private void SSLHTTPDirect(bool SSLRequired)
{
if (int.Parse(ConfigurationManager.AppSettings["UseSSL"].ToString()) == 1)
{
bool IsOnSSL = HttpContext.Current.Request.Url.Scheme.ToLower() == "https";
if (SSLRequired && !IsOnSSL)
Response.Redirect(ConfigurationManager.AppSettings["SecureDomainRoot"] + "" + Request.RawUrl);
else if (!SSLRequired && IsOnSSL)
Response.Redirect(ConfigurationManager.AppSettings["MasterDomainRoot"] + "" + Request.RawUrl);
}
}
On my SSL required pages, it works fine. I just do Master.ForceSSL()
and it redirects to the secure connection if they are on HTTP.
The problem is, I want to redirect all other pages to HTTP if they are on HTTPS without having to manually trawl through the pages adding the function call to ForceUnsecure()
.
Whatever I try, I can't seem to work out from the Master page if the ForceSSL() function has been called (using flags and such). Ideally I want something like
if(!SSLRequired && OnHTTPS){ForceUnsecure()}
But whatever I try the master page seems to perform all its checks BEFORE the content page makes a call to ForceSSL()
. So I can never know the values the content page is setting.