. NET common tool class--cookies operation class

Source: Internet
Author: User
Tags allkeys

Using System;
Using System.Collections;
Using System.Collections.Generic;
Using System.Collections.Specialized;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;

Namespace ZC. Utils
{
<summary>
<para> </para>
Common tool class--cookies operation class
<para>-------------------------------------------------------------------</para>
<para> Writecookie: Create a Cookie object and assign value or value collection [+4 overload]</para>
<para> GetCookie: Reads the value of a cookie object, returns the value of value, or returns null</para> if the object does not exist.
<para> Delcookie: Delete Cookie Object </para>
</summary>
public class Cookieshelper
{

#region Create a Cookie object and assign value values
<summary>
Creating a Cookie object and assigning value values
</summary>
<param name= "Cookiesname" >cookie object name </param>
<param name= "Iexpires" >cookie object effective time (in seconds), 1 means permanent, 0 and negative numbers mean no valid time, greater than or equal to 2 means the specific effective seconds, 31.536 million seconds =1 year = (60*60*24*365 ),</param>
<param name= "Cookiesvalue" >cookie Object value </param>
public static void Writecookies (string cookiesname, int iexpires, string cookiesvalue)
{
HttpCookie Objcookie = new HttpCookie (Cookiesname.trim ());
Objcookie.value = Md5helper.desencrypt (Cookiesvalue.trim ()); Encrypted storage
if (Iexpires > 0)
{
if (iexpires = = 1)
{
Objcookie.expires = DateTime.MaxValue;
}
Else
{
Objcookie.expires = DateTime.Now.AddMinutes (iexpires);
}
}
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Objcookie);
}

<summary>
Creating a Cookie object and assigning value values
</summary>
<param name= "Cookiesname" >cookie object name </param>
<param name= "Iexpires" >cookie object effective time (in seconds), 1 means permanent, 0 and negative numbers mean no valid time, greater than or equal to 2 means the specific effective seconds, 31.536 million seconds =1 year = (60*60*24*365 ),</param>
<param name= "Cookiesvalue" >cookie Object value </param>
<param name= "Cookiesdomain" > Scope </param>
public static void Writecookies (string cookiesname, int iexpires, string cookiesvalue, String cookiesdomain)
{
HttpCookie Objcookie = new HttpCookie (Cookiesname.trim ());
Objcookie.value = Md5helper.desencrypt (Cookiesvalue.trim ()); Encrypted storage
Objcookie.domain = Cookiesdomain.trim ();
if (Iexpires > 0)
{
if (iexpires = = 1)
{
Objcookie.expires = DateTime.MaxValue;
}
Else
{
Objcookie.expires = DateTime.Now.AddMinutes (iexpires);
}
}
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Objcookie);
}

<summary>
Create a Cookie object and assign multiple key key values
Set the key/value as follows:
NameValueCollection MyCol = new NameValueCollection ();
Mycol.add ("Red", "Rojo");
Mycol.add ("Green", "Verde");
Mycol.add ("Blue", "Azul");
Mycol.add ("Red", "rouge"); Result "Red:rojo,rouge;green:verde;blue:azul"
</summary>
<param name= "Cookiesname" >cookie object name </param>
<param name= "Iexpires" >cookie object effective time (in seconds), 1 means permanent, 0 and negative numbers mean no valid time, greater than or equal to 2 means the specific effective seconds, 31.536 million seconds =1 year = (60*60*24*365 ),</param>
<param name= "cookieskeyvaluecollection" > key/value pairs collection </param>
public static void Writecookies (string cookiesname, int iexpires, NameValueCollection cookieskeyvaluecollection)
{
HttpCookie Objcookie = new HttpCookie (Cookiesname.trim ());
foreach (String key in Cookieskeyvaluecollection.allkeys)
{
Objcookie[key] = Md5helper.desencrypt (Cookieskeyvaluecollection[key]. Trim ());
}
if (Iexpires > 0)
{
if (iexpires = = 1)
{
Objcookie.expires = DateTime.MaxValue;
}
Else
{
Objcookie.expires = DateTime.Now.AddSeconds (iexpires);
}
}
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Objcookie);
}

<summary>
Create a Cookie object and assign multiple key key values
Set the key/value as follows:
NameValueCollection MyCol = new NameValueCollection ();
Mycol.add ("Red", "Rojo");
Mycol.add ("Green", "Verde");
Mycol.add ("Blue", "Azul");
Mycol.add ("Red", "rouge"); Result "Red:rojo,rouge;green:verde;blue:azul"
</summary>
<param name= "Cookiesname" >cookie object name </param>
<param name= "Iexpires" >cookie object effective time (in seconds), 1 means permanent, 0 and negative numbers mean no valid time, greater than or equal to 2 means the specific effective seconds, 31.536 million seconds =1 year = (60*60*24*365 ),</param>
<param name= "cookieskeyvaluecollection" > key/value pairs collection </param>
<param name= "Cookiesdomain" > Scope </param>
public static void Writecookies (string cookiesname, int iexpires, NameValueCollection cookieskeyvaluecollection, string Cookiesdomain)
{
HttpCookie Objcookie = new HttpCookie (Cookiesname.trim ());
foreach (String key in Cookieskeyvaluecollection.allkeys)
{
Objcookie[key] = Md5helper.desencrypt (Cookieskeyvaluecollection[key]. Trim ());
}
Objcookie.domain = Cookiesdomain.trim ();
if (Iexpires > 0)
{
if (iexpires = = 1)
{
Objcookie.expires = DateTime.MaxValue;
}
Else
{
Objcookie.expires = DateTime.Now.AddSeconds (iexpires);
}
}
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Objcookie);
}

#endregion

#region reads the value of a cookie object, returns the value of value, or returns NULL if the object does not exist.
<summary>
Reads the value of a cookie object, returns the value of value, or returns NULL if the object does not exist
</summary>
<param name= "Cookiesname" >cookie object name </param>
<returns> returns the value of the object, returns the value, or returns null</returns> if the object does not exist.
public static string GetCookies (String cookiesname)
{
if (httpcontext.current.request.cookies[cookiesname] = = null)
{
return null;
}
Else
{
Return Md5helper.desdecrypt (Httpcontext.current.request.cookies[cookiesname]. Value);
}
}

<summary>
Reads the value of a cookie object, returns the value of value, or returns NULL if the object does not exist
</summary>
<param name= "Cookiesname" >cookie object name </param>
<param name= "KeyName" > Key value </param>
<returns> returns the value of the object, returns the value, or returns null</returns> if the object does not exist.
public static string GetCookies (String cookiesname, String KeyName)
{
if (httpcontext.current.request.cookies[cookiesname] = = null)
{
return null;
}
Else
{
String strobjvalue = Md5helper.desdecrypt (Httpcontext.current.request.cookies[cookiesname]. Value);
String strKeyName2 = KeyName + "=";
if (Strobjvalue.indexof (strKeyName2) = =-1)
{
return null;
}
Else
{
Return Md5helper.desdecrypt (Httpcontext.current.request.cookies[cookiesname][keyname]);
}
}
}
#endregion

#region Delete a Cookie object
<summary>
Delete Cookie Object
</summary>
<param name= "Cookiesname" >cookie object name </param>
public static void Delcookie (String cookiesname)
{
HttpCookie Objcookie = new HttpCookie (Cookiesname.trim ());
Objcookie.expires = DateTime.Now.AddYears (-5);
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Objcookie);
}
#endregion
}
}

. NET common tool class--cookies operation class

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.