public static class WebSiteUtility
{
///
/// This method will examine the current URL for use of https. If https:// isn't present,
/// the page will reset itself to the secure url. This does NOT happen for localhost.
///
///
public static void RedirectSSL(string page)
{
//this is the current url
string currentUrl = HttpContext.Current.Request.Url.ToString();
string secureRoot = GetSecureRoot();
if (!secureRoot.EndsWith("/"))
secureRoot += "/";
secureRoot += page;
//don't redirect if this is localhost
if (!currentUrl.Contains("localhost"))
{
if (!currentUrl.StartsWith("https://"))
HttpContext.Current.Response.Redirect(secureRoot);
}
}
///
/// Returns an SSL-enabled URL
///
///
public static string GetSecureRoot()
{
//this is the current url
string siteUrl = GetSiteRoot();
if (!siteUrl.ToLower().StartsWith("https://"))
siteUrl = siteUrl.Replace("http:", "https:");
return siteUrl;
}
///
/// Returns a regular URL
///
///
public static string GetNonSSLRoot()
{
//this is the current url
return GetSiteRoot();
}
public static string GetSiteRoot()
{
string Port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
if (Port == null || Port == "80" || Port == "443")
Port = "";
else
Port = ":" + Port;
string Protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
if (Protocol == null || Protocol == "0")
Protocol = "http://";
else
Protocol = "https://";
string appPath = HttpContext.Current.Request.ApplicationPath;
if (appPath == "/")
appPath = "";
string sOut = Protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + Port + appPath;
return sOut;
}
}
|