Perfect code for cookies under Asp.net

Source: Internet
Author: User
Tags set cookie

CopyCode The 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 );
}
}
}
<br>
<br>
<br>
<br>
<br>
<br>
<br>
/// <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
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.