Copy codeThe Code is as follows:
Using System;
Using System. Web;
Namespace Moosoft. OA. Public
{
/// <Summary>
/// Cookie help class
/// </Summary>
Public class CookiesHelper
{
# Region obtain Cookie
/// <Summary>
/// Obtain the Cookie value
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Returns> </returns>
Public static string GetCookieValue (string cookieName)
{
Return GetCookieValue (cookieName, null );
}
/// <Summary>
/// Obtain the Cookie value
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string GetCookieValue (string cookieName, string key)
{
HttpRequest request = HttpContext. Current. Request;
If (request! = Null)
Return GetCookieValue (request. Cookies [cookieName], key );
Return "";
}
/// <Summary>
/// Obtain the Cookie's subkey Value
/// </Summary>
/// <Param name = "cookie"> </param>
/// <Param name = "key"> </param>
/// <Returns> </returns>
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 "";
}
/// <Summary>
/// Obtain the Cookie
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Returns> </returns>
Public static HttpCookie GetCookie (string cookieName)
{
HttpRequest request = HttpContext. Current. Request;
If (request! = Null)
Return request. Cookies [cookieName];
Return null;
}
# Endregion
# Region Delete Cookie
/// <Summary>
/// Delete a Cookie
/// </Summary>
/// <Param name = "cookieName"> </param>
Public static void RemoveCookie (string cookieName)
{
RemoveCookie (cookieName, null );
}
/// <Summary>
/// Delete the Cookie subkey
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "key"> </param>
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 setting/modifying cookies
/// <Summary>
/// Set the Cookie subkey Value
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Public static void SetCookie (string cookieName, string key, string value)
{
SetCookie (cookieName, key, value, null );
}
/// <Summary>
/// Set the Cookie value
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Public static void SetCookie (string key, string value)
{
SetCookie (key, null, value, null );
}
/// <Summary>
/// Set the Cookie value and expiration time
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
/// <Param name = "expires"> </param>
Public static void SetCookie (string key, string value, DateTime expires)
{
SetCookie (key, null, value, expires );
}
/// <Summary>
/// Set the Cookie expiration time
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "expires"> </param>
Public static void SetCookie (string cookieName, DateTime expires)
{
SetCookie (cookieName, null, null, expires );
}
/// <Summary>
/// Set Cookie
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
/// <Param name = "expires"> </param>
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 add Cookie
/// <Summary>
/// Add Cookie
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Public static void AddCookie (string key, string value)
{
AddCookie (new HttpCookie (key, value ));
}
/// <Summary>
/// Add Cookie
/// </Summary>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
/// <Param name = "expires"> </param>
Public static void AddCookie (string key, string value, DateTime expires)
{
HttpCookie cookie = new HttpCookie (key, value );
Cookie. Expires = expires;
AddCookie (cookie );
}
/// <Summary>
/// Add as Cookie. Values set
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
Public static void AddCookie (string cookieName, string key, string value)
{
HttpCookie cookie = new HttpCookie (cookieName );
Cookie. Values. Add (key, value );
AddCookie (cookie );
}
/// <Summary>
/// Add as a Cookie set
/// </Summary>
/// <Param name = "cookieName"> Cookie name </param>
/// <Param name = "expires"> expiration time </param>
Public static void AddCookie (string cookieName, DateTime expires)
{
HttpCookie cookie = new HttpCookie (cookieName );
Cookie. Expires = expires;
AddCookie (cookie );
}
/// <Summary>
/// Add as Cookie. Values set
/// </Summary>
/// <Param name = "cookieName"> </param>
/// <Param name = "key"> </param>
/// <Param name = "value"> </param>
/// <Param name = "expires"> </param>
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 );
}
/// <Summary>
/// Add Cookie
/// </Summary>
/// <Param name = "cookie"> </param>
Public static void AddCookie (HttpCookie cookie)
{
HttpResponse response = HttpContext. Current. Response;
If (response! = Null)
{
// Specify whether client scripts can be accessed [false by default]
Cookie. HttpOnly = true;
// Specify a uniform Path, which can be accessed through memory
Cookie. Path = "/";
// Configure cross-origin so that all other second-level domain names can be accessed.
// Cookie. Domain = "chinesecoo.com ";
Response. AppendCookie (cookie );
}
}
# Endregion
}
}