Пример #1
0
        protected override void HandleNonHttpsRequest(AuthorizationContext filterContext)
        {
            if (String.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                if (filterContext.HttpContext.Request.Url.AbsoluteUri.IndexOf("https://", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    string path = filterContext.HttpContext.Request.Path;
                    string host = filterContext.HttpContext.Request.Url.Host;

                    if (!HttpPort.Equals("80"))
                    {
                        host = string.Format("{0}:{1}", host, HttpPort);
                    }

                    string url = string.Format("http://{0}{1}", host, path);
                    filterContext.Result = new RedirectResult(url);
                }
            }
        }
Пример #2
0
        private static void SetHttps(AuthorizationContext filterContext)
        {
            string path = filterContext.HttpContext.Request.Path;

            string host = filterContext.HttpContext.Request.Url.Host;
            string port = HttpsPort;

            if (!HttpPort.Equals(filterContext.HttpContext.Request.Url.Port.ToString()))
            {
                HttpPort = filterContext.HttpContext.Request.Url.Port.ToString();
            }

            if (!string.IsNullOrEmpty(port))
            {
                host = string.Format("{0}:{1}", host, port);
            }

            if (!host.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
            {
                host = "www." + host;
            }

            filterContext.Result = new RedirectResult(string.Format("https://{0}{1}", host, path));
        }