asp.net cookies Operation Perfect Code _ practical skills

Source: Internet
Author: User
Tags httpcontext set cookie setcookie
Copy Code code as follows:

Using System;
Using System.Web;
Namespace Moosoft.OA.Public
{
<summary>
Cookie Help Class
</summary>
public class Cookieshelper
{
#region Get Cookies
<summary>
Get the value of a cookie
</summary>
<param name= "CookieName" ></param>
<returns></returns>
public static string Getcookievalue (String cookiename)
{
Return Getcookievalue (cookiename, NULL);
}
<summary>
Get the value of a cookie
</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>
Get the subkey value of the cookie
</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) && cookies. HasKeys)
Return cookie. Values[key];
Else
Return cookie. Value;
}
Return "";
}
<summary>
Get cookies
</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 Cookies
<summary>
Delete Cookies
</summary>
<param name= "CookieName" ></param>
public static void Removecookie (String cookiename)
{
Removecookie (cookiename, NULL);
}
<summary>
Delete a subkey for a cookie
</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) && cookies. HasKeys)
Cookie. Values.remove (key);
Else
Response. Cookies.remove (CookieName);
}
}
}
#endregion
#region Setting/Modifying Cookies
<summary>
Set the value of a cookie subkey
</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 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 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 cookies
</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) && cookies. 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 Cookies
<summary>
Add cookies
</summary>
<param name= "Key" ></param>
<param name= "Value" ></param>
public static void Addcookie (string key, String value)
{
Addcookie (new HttpCookie (key, value));
}
<summary>
Add cookies
</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 Collection
</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 Cookie Collection
</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 Collection
</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 cookies
</summary>
<param name= "Cookie" ></param>
public static void Addcookie (HttpCookie cookie)
{
HttpResponse response = HttpContext.Current.Response;
if (response!= null)
{
Specify whether client script can access [default to FALSE]
Cookie. HttpOnly = true;
Specifies a unified path, which allows you to save and access
Cookie. Path = "/";
Set Cross-domain so that it can be accessed under Other level two domain names.
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.