asp.Net下cookies操作完璧コード
7338 ワード
using System;
using System.Web;
namespace Moosoft.OA.Public
{
///
/// Cookie
///
public class CookiesHelper
{
#region Cookie
///
/// Cookie
///
///
///
public static string GetCookieValue(string cookieName)
{
return GetCookieValue(cookieName, null);
}
///
/// Cookie
///
///
///
///
public static string GetCookieValue(string cookieName, string key)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return GetCookieValue(request.Cookies[cookieName], key);
return "";
}
///
/// Cookie
///
///
///
///
public static string GetCookieValue(HttpCookie cookie, string key)
{
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
return cookie.Values[key];
else
return cookie.Value;
}
return "";
}
///
/// Cookie
///
///
///
public static HttpCookie GetCookie(string cookieName)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return null;
}
#endregion
#region Cookie
///
/// Cookie
///
///
public static void RemoveCookie(string cookieName)
{
RemoveCookie(cookieName, null);
}
///
/// Cookie
///
///
///
public static void RemoveCookie(string cookieName, string key)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Remove(key);
else
response.Cookies.Remove(cookieName);
}
}
}
#endregion
#region / Cookie
///
/// Cookie
///
///
///
///
public static void SetCookie(string cookieName, string key, string value)
{
SetCookie(cookieName, key, value, null);
}
///
/// Cookie
///
///
///
public static void SetCookie(string key, string value)
{
SetCookie(key, null, value, null);
}
///
/// Cookie
///
///
///
///
public static void SetCookie(string key, string value, DateTime expires)
{
SetCookie(key, null, value, expires);
}
///
/// Cookie
///
///
///
public static void SetCookie(string cookieName, DateTime expires)
{
SetCookie(cookieName, null, null, expires);
}
///
/// Cookie
///
///
///
///
///
public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
HttpCookie cookie = response.Cookies[cookieName];
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
cookie.Values.Set(key, value);
else
if (!string.IsNullOrEmpty(value))
cookie.Value = value;
if (expires != null)
cookie.Expires = expires.Value;
response.SetCookie(cookie);
}
}
}
#endregion
#region Cookie
///
/// Cookie
///
///
///
public static void AddCookie(string key, string value)
{
AddCookie(new HttpCookie(key, value));
}
///
/// Cookie
///
///
///
///
public static void AddCookie(string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(key, value);
cookie.Expires = expires;
AddCookie(cookie);
}
///
/// Cookie.Values
///
///
///
///
public static void AddCookie(string cookieName, string key, string value)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Values.Add(key, value);
AddCookie(cookie);
}
///
/// Cookie
///
/// Cookie
///
public static void AddCookie(string cookieName, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
AddCookie(cookie);
}
///
/// Cookie.Values
///
///
///
///
///
public static void AddCookie(string cookieName, string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie(cookieName);
cookie.Expires = expires;
cookie.Values.Add(key, value);
AddCookie(cookie);
}
///
/// Cookie
///
///
public static void AddCookie(HttpCookie cookie)
{
HttpResponse response = HttpContext.Current.Response;
if (response != null)
{
// [ false]
cookie.HttpOnly = true;
// Path,
cookie.Path = "/";
// ,
//cookie.Domain = "chinesecoo.com";
response.AppendCookie(cookie);
}
}
#endregion
}
}