using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pcb.Common
{
using System.Web;
public static class CookieUtils
{
///
/// 根据Cookie 名称获取cookie的value,
/// 如有异常,返回所属类型的默认值
///
///
///
///
///
public static T GetCookieOrDefault(this string cookieName, string key = null)
{
try
{
return GetCookie(cookieName, key: key);
}
//catch (Exception ex)
catch
{
//LogUtils.ExceptionLog("读取" + cookieName + " [Cookie]的信息时发生异常", ex);
}
return default(T);
}
///
/// 从一个Cookie中读取字符串值。
/// 如有异常,将抛出异常信息。
///
///
///
public static T GetCookie(this string cookieName, string key = null)
{
if (!string.IsNullOrEmpty(cookieName))
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
{
return GetCookie(request.Cookies[cookieName], key: key);
}
}
return default(T);
}
///
/// 从一个Cookie中读取字符串值。
///
///
///
public static T GetCookie(this HttpCookie cookie, string key = null)
{
if (cookie == null)
return default(T);
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
{
return cookie.Values[key].UrlDecode().ChangeValueDefault();
}
return cookie.Value.UrlDecode().ChangeValueDefault();
}
public static HttpCookie GetCookieInfo(this string cookieName, string key = null)
{
if (!string.IsNullOrEmpty(cookieName) && HttpContext.Current != null)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
{
return request.Cookies[cookieName];
}
}
return null;
}
///
/// 从一个Cookie中读取【JSON字符串】值并反序列化成一个对象,用于读取复杂对象
///
///
///
///
public static T FromJson(this HttpCookie cookie)
{
if (cookie == null)
return default(T);
return cookie.Value.UrlDecode().FromJson();
}
///
/// 将一个对象写入到Cookie
///
///
///
///
///
///
public static void SetCookie(this object obj, string name, string key = "", string domain = "", string path = "", DateTime? expries = null, bool httpOnly = false)
{
if (obj == null)
throw new ArgumentNullException("obj");
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
HttpCookie cookie;
var cookieVal = HttpContext.Current.Request.Cookies[name];
if (cookieVal != null)
{
cookie = cookieVal;
}
else
{
cookie = new HttpCookie(name);
}
if (!string.IsNullOrEmpty(key))
cookie[key] = obj.ToString().UrlEncode();
else
cookie.Value = obj.ToString().UrlEncode();
cookie.Path = string.IsNullOrEmpty(path) ? "/" : path;
if (!string.IsNullOrEmpty(domain))
{
cookie.Domain = domain;
}
if (expries != null && expries.HasValue)
{
cookie.Expires = expries.Value;
}
cookie.HttpOnly = httpOnly;
HttpContext.Current.Response.Cookies.Add(cookie);
}
public static void SetCookie(this System.Net.Cookie val, HttpContext current = null)
{
if (current == null)
{
current = HttpContext.Current;
}
if (current != null && val != null)
{
HttpCookie cookie = new HttpCookie(val.Name);
cookie.Value = val.Value;
cookie.Domain = val.Domain;
cookie.Expires = val.Expires;
cookie.HttpOnly = val.HttpOnly;
cookie.Path = val.Path;
cookie.Secure = val.Secure;
current.Response.Cookies.Add(cookie);
}
}
///
/// 删除指定的Cookie
///
///
public static void DeleteCookie(this string name, string domainName = null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
HttpCookie cookie = new HttpCookie(name);
if (!string.IsNullOrEmpty(domainName))
{
cookie.Domain = domainName;
}
// 删除Cookie,其实就是设置一个【过期的日期】
cookie.Expires = new DateTime(1900, 1, 1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}