CookieHelper,CacheHelper,MD5Helper

public class CookieHelper {
        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="domain">作用域,为空就不写入作用域</param>
        public static void SetCookie(String cookieName, String cookieValue, string domain) {
            if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return;
            if (HttpContext.Current != null) {
                HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
                if (domain.Length > 0) {
                    cookie.Domain = domain;
                }
                cookie.HttpOnly = true;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="domain">作用域,为空就不写入作用域</param>
        /// <param name="day">有效时间</param>
        public static void SetCookie(String cookieName, String cookieValue, string domain, int day) {
            if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return;
            if (HttpContext.Current != null) {
                HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
                if (domain.Length > 0) {
                    cookie.Domain = domain;
                }
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now.AddDays(day);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 设置cookie过期
        /// </summary>
        /// <param name="cookieName">需要过期的cookie名称</param>
        public static void ExpireCookie(String cookieName) {
            if (String.IsNullOrEmpty(cookieName)) return;
            if (HttpContext.Current != null) {
                HttpCookie cookie = new HttpCookie(cookieName, string.Empty);
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now.AddYears(-5);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 获取对应Cookie名称的值
        /// </summary>
        /// <param name="cookieName">Cookie 的名称</param>
        /// <returns></returns>
        public static string GetCookie(string cookieName) {
            if (string.IsNullOrEmpty(cookieName)) return string.Empty;
            if (System.Web.HttpContext.Current == null) return string.Empty;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return string.Empty;
            else return System.Web.HttpContext.Current.Request.Cookies[cookieName].Value;
        }
        /// <summary>
        /// 判断对应的Cookie是否存在
        /// </summary>
        /// <param name="cookieName">Cookie 的名称</param>
        /// <returns></returns>
        public static bool ExistCookie(string cookieName) {

            if (string.IsNullOrEmpty(cookieName) || System.Web.HttpContext.Current == null) return false;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return false;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value == null) return false;
            return (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value.Length > 0);
        }

    }


public class CacheHelper {
        /// <summary>
        /// 判断缓存是否存在
        /// </summary>
        /// <param name="key">需要判断的缓存名称</param>
        /// <returns></returns>
        public static bool IsExistCache(string key) {
            if (string.IsNullOrEmpty(key)) return false;
            if (System.Web.HttpRuntime.Cache[key] == null) return false;
            return true;
        }
        /// <summary>
        /// 读取缓存
        /// </summary>
        public static object GetCache(string key) {   ///检测对象是否为空
            if (System.Web.HttpRuntime.Cache == null) return null;
            if (string.IsNullOrEmpty(key)) return null;
            ///获取数据
            return System.Web.HttpRuntime.Cache[key];
        }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">缓存名称</param>
        /// <param name="obj">缓存对象</param>
        /// <param name="hour">有效时间</param>
        public static void AddCache(string key, object obj,int hour) {
            if (string.IsNullOrEmpty(key)) return;
            if (System.Web.HttpRuntime.Cache[key] == null) {
                System.Web.HttpRuntime.Cache.Insert(key, obj, null, DateTime.Now.AddHours(hour), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            else {
                System.Web.HttpRuntime.Cache.Remove(key);
                System.Web.HttpRuntime.Cache.Insert(key, obj, null, DateTime.Now.AddHours(hour), System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">需要移除的缓存</param>
        public static void RemoveCache(string key) {
            if (string.IsNullOrEmpty(key)) return;
            if (System.Web.HttpRuntime.Cache[key] != null) {
                System.Web.HttpRuntime.Cache.Remove(key);
            }
            return;
        }
    }


public class MD5Helper {
        /// <summary>
        /// MD5散列
        /// </summary>
        public static string MD5(string inputStr) {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] hashByte = md5.ComputeHash(Encoding.UTF8.GetBytes(inputStr));
            StringBuilder sb = new StringBuilder();
            foreach (byte item in hashByte)
                sb.Append(item.ToString("X").PadLeft(2, '0'));
            return sb.ToString();
        }
        /// <summary>
        /// 创建密码MD5
        /// </summary>
        /// <param name="password">原密码</param>
        /// <param name="salt">盐值</param>
        /// <returns></returns>
        public static string CreatePasswordMd5(string password, string salt) {
            return MD5(string.Format("{0}{1}",password,salt));
        }
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值