예제 #1
0
 /// <summary>
 /// 清掉一个key
 /// </summary>
 /// <param name="key"></param>
 public static void Clear(string key)
 {
     HttpContextHelper.GetCurrent().Response.Cookies.Append(key, null, new CookieOptions()
     {
         Expires = DateTime.Now.AddDays(-1)
     });
 }
예제 #2
0
        /// <summary>
        /// GetCurrentSource
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentSource()
        {
            var source = HttpUtility.HtmlEncode(HttpContextHelper.GetCurrent().Request.Query["name"]);

            if (string.IsNullOrEmpty(source))
            {
                source = CookieHelper.Get(CookieHelper.WebSource);
            }
            else
            {
                CookieHelper.Set(CookieHelper.WebSource, source);
            }
            return(source);
        }
예제 #3
0
        /// <summary>
        /// GetRefererUrl
        /// </summary>
        /// <returns></returns>
        public static string GetRefererUrl()
        {
            var headers = HttpContextHelper.GetCurrent().Request.Headers;

            if (headers.ContainsKey("Referer"))
            {
                return(headers["Referer"]);
            }
            if (headers.ContainsKey("referer"))
            {
                return(headers["referer"]);
            }

            return("");
        }
예제 #4
0
        /// <summary>
        /// 获取当前请求的url
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUrl()
        {
            var path  = HttpContextHelper.GetCurrent().Request.Headers["X-Original-URL"];
            var proto = HttpContextHelper.GetCurrent().Request.Headers["X-Client-Proto"];
            var url   = "";

            // LogHelper.Debug("1GetCurrentUrl path=" + path);
            // LogHelper.Debug("1GetCurrentUrl proto=" + proto);
            if (string.IsNullOrEmpty(path))
            {
                var request = HttpContextHelper.GetCurrent().Request;
                //为空, 走正常
                url = string.Concat(
                    request.Scheme,
                    "://",
                    request.Host.ToUriComponent(),
                    request.PathBase.ToUriComponent(),
                    request.Path.ToUriComponent(),
                    request.QueryString.ToUriComponent());
            }
            else
            {
                url = ConfigHelper.Get(ConfigHelper.WebHost) + path;
            }

            if (string.IsNullOrEmpty(url))
            {
                url = ConfigHelper.Get(ConfigHelper.WebHost);
            }
            //url = url.ToLower();
            if (!string.IsNullOrEmpty(proto))
            {
                if (url.IndexOf("http://", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    url = Regex.Replace(url, "http://", proto + "://", RegexOptions.IgnoreCase);
                }
                else if (url.IndexOf("https://", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    url = Regex.Replace(url, "https://", proto + "://", RegexOptions.IgnoreCase);
                }
            }
            //LogHelper.Debug("1GetCurrentUrl url=" + url);
            return(url);
        }
예제 #5
0
        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <returns>若失败则返回回送地址</returns>
        public static string GetIp()
        {
            string userHostAddress   = "";
            var    httpXForwardedFor = HttpContextHelper.GetCurrent().Request.Headers["X-Forwarded-For"].ToString();

            //如果客户端使用了代理服务器,则利用HTTP_X_FORWARDED_FOR找到客户端IP地址
            if (!string.IsNullOrEmpty(httpXForwardedFor))
            {
                userHostAddress = httpXForwardedFor.Split(':')[0].Trim();
            }
            //前两者均失败,则利用Request.UserHostAddress属性获取IP地址,但此时无法确定该IP是客户端IP还是代理IP
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContextHelper.GetCurrent().Connection.RemoteIpAddress.ToString();
            }
            //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)
            if (!string.IsNullOrEmpty(userHostAddress) && IsIp(userHostAddress))
            {
                return(userHostAddress);
            }
            return("127.0.0.1");
        }
예제 #6
0
 /// <summary>
 /// 从cookie中拿到一个值
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string Get(string key)
 {
     if (HttpContextHelper.GetCurrent().Request.Cookies.ContainsKey(key))
     {
         var source = HttpContextHelper.GetCurrent().Request.Cookies[key];
         if (string.IsNullOrEmpty(source))
         {
             return("");
         }
         var result = "";
         try
         {
             result = RsaHelper.DecryptData(source, RsaHelper.privateKey);
         }
         catch
         {
             // ignored
         }
         return(result);
     }
     return("");
 }
예제 #7
0
        /// <summary>
        /// 往cookie中设置一个值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static void Set(string key, string value)
        {
            var result = "";

            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    result = RsaHelper.EncryptData(value, RsaHelper.publicKey);
                }
                catch
                {
                    // ignored
                }
            }


            HttpContextHelper.GetCurrent().Response.Cookies.Append(key, result, new CookieOptions()
            {
                Domain = ConfigHelper.Get(ConfigHelper.CookieDomain),
                Path   = "/"
            });
        }