示例#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>
        /// 用来生成一个导出的文件名
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetExportFileName(string dir, out string path)
        {
            path = "/Files/Export/" + dir;
            var guide = Guid.NewGuid().ToString();

            path += "/" + guide + ".xlsx";
            return(HttpContextHelper.GetPath(path));
        }
示例#3
0
        /// <summary>
        /// 取得一个新的连接
        /// </summary>
        /// <returns></returns>
        public static IDbConnection GetNewConnection()
        {
            IDbConnection conn = new MySqlConnection(ConfigHelper.Get(ConfigHelper.DbConnection));

            HttpContextHelper.Set(HttpContextHelper.DbConn, conn);
            conn.Open();
            return(conn);
        }
示例#4
0
        /// <summary>
        /// 回滚事务
        /// </summary>
        public static void RollBackTrans()
        {
            IDbTransaction transaction = HttpContextHelper.Get <IDbTransaction>(HttpContextHelper.DbTransaction);

            if (transaction != null)
            {
                transaction.Rollback();
                HttpContextHelper.Set(HttpContextHelper.DbTransaction, null);
            }
        }
示例#5
0
        /// <summary>
        /// 提交事务
        /// </summary>
        public static void SubmitTrans()
        {
            IDbTransaction transaction = HttpContextHelper.Get <IDbTransaction>(HttpContextHelper.DbTransaction);

            if (transaction != null)
            {
                transaction.Commit();
                HttpContextHelper.Set(HttpContextHelper.DbTransaction, null);
            }
        }
示例#6
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);
        }
示例#7
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("");
        }
示例#8
0
        /// <summary>
        /// 获取当前上下文中的数据库连接, 要是没有, 则返回新的
        /// </summary>
        /// <param name="create">如果为true会创建</param>
        /// <param name="type">如果为true会创建</param>
        /// <returns></returns>
        public static IDbConnection GetCurrentConnection(bool create = true)
        {
            IDbConnection conn = HttpContextHelper.Get <IDbConnection>(HttpContextHelper.DbConn);

            if (conn == null)
            {
                if (create)
                {
                    return(GetNewConnection());
                }

                return(null);
            }
            return(conn);
        }
示例#9
0
        /// <summary>
        /// 下载一个文件
        /// </summary>
        /// <param name="url"></param>
        public static string DownFile(string url, string name)
        {
            WebClient client = new WebClient();

            try
            {
                Stream       str        = client.OpenRead(url);
                StreamReader reader     = new StreamReader(str);
                byte[]       mbyte      = new byte[1000000];
                int          allmybyte  = (int)mbyte.Length;
                int          startmbyte = 0;

                if (allmybyte <= 0)
                {
                    return("");
                }

                while (allmybyte > 0)
                {
                    int m = str.Read(mbyte, startmbyte, allmybyte);
                    if (m == 0)
                    {
                        break;
                    }
                    startmbyte += m;
                    allmybyte  -= m;
                }

                reader.Dispose();
                str.Dispose();

                string path = HttpContextHelper.GetPath(name);
                if (File.Exists(path))
                {
                    return(path);
                }

                FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                fstr.Write(mbyte, 0, startmbyte);
                fstr.Flush();
                fstr.Close();
                return(path);
            }
            catch
            {
                return("");
            }
        }
示例#10
0
        /// <summary>
        /// 获取当前的事务(整个请求全局有效)
        /// </summary>
        /// <param name="createNew"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IDbTransaction GetCurrentTransaction(bool createNew = true)
        {
            IDbTransaction transaction = HttpContextHelper.Get <IDbTransaction>(HttpContextHelper.DbTransaction);

            if (transaction == null && createNew)
            {
                var conn = GetCurrentConnection();
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();
                }
                transaction = conn.BeginTransaction();
                HttpContextHelper.Set(HttpContextHelper.DbTransaction, transaction);
            }
            return(transaction);
        }
示例#11
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);
        }
示例#12
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");
        }
示例#13
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("");
 }
示例#14
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   = "/"
            });
        }