/// <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));
}
}