<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Switch from HTTP to HTTPS automatically</Title>
      <Shortcut>SwitchfromHTTPtoHTTPSautomatically</Shortcut>
      <Description>Switch from HTTP to HTTPS automatically [C#]</Description>
      <Author>Zepho Zep</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=28699d1b-c35f-4def-95c1-a53e06928e02</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[    public static class WebSiteUtility
    {
        /// <summary>
        /// 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.
        /// </summary>
        /// <param name="page"></param>
        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);
            }
        }

        /// <summary>
        /// Returns an SSL-enabled URL
        /// </summary>
        /// <returns></returns>
        public static string GetSecureRoot()
        {
            //this is the current url 
            string siteUrl = GetSiteRoot();
            if (!siteUrl.ToLower().StartsWith("https://"))
                siteUrl = siteUrl.Replace("http:", "https:");
            return siteUrl;
        }

        /// <summary>
        /// Returns a regular URL
        /// </summary>
        /// <returns></returns>
        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;
        }
    }]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>