CopyCode The Code is as follows: using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Data;
Using system. configuration;
Namespace jhgl. Smart
{
/// <Summary>
/// Cookie operation class
/// </Summary>
Public class cookie
{
/// <Summary>
/// Save a cookie
/// </Summary>
/// <Param name = "cookiename"> cookie name </param>
/// <Param name = "cookievalue"> cookie value </param>
/// <Param name = "cookietime"> cookie expiration time (hours). 0 indicates that the page is disabled. </param>
Public static void savecookie (string cookiename, string cookievalue, double cookietime)
{
Httpcookie mycookie = new httpcookie (cookiename );
Datetime now = datetime. now;
Mycookie. value = cookievalue;
If (cookietime! = 0)
{
// There are two methods. If you set the cookie time in the first method, disabling the browser will not automatically clear the cookie.
// If the cookie time is not set in method 2, the cookie is automatically cleared when the browser is closed, but the validity period is
// How long has it not been confirmed.
Mycookie. expires = now. adddays (cookietime );
If (httpcontext. Current. response. Cookies [cookiename]! = NULL)
Httpcontext. Current. response. Cookies. Remove (cookiename );
Httpcontext. Current. response. Cookies. Add (mycookie );
}
Else
{
If (httpcontext. Current. response. Cookies [cookiename]! = NULL)
Httpcontext. Current. response. Cookies. Remove (cookiename );
Httpcontext. Current. response. Cookies. Add (mycookie );
}
}
/// <Summary>
/// Obtain cookievalue
/// </Summary>
/// <Param name = "cookiename"> cookie name </param>
/// <Returns> cookie value </returns>
Public static string getcookie (string cookiename)
{
Httpcookie mycookie = new httpcookie (cookiename );
Mycookie = httpcontext. Current. Request. Cookies [cookiename];
If (mycookie! = NULL)
Return mycookie. value;
Else
Return NULL;
}
/// <Summary>
/// Clear cookievalue
/// </Summary>
/// <Param name = "cookiename"> cookie name </param>
Public static void clearcookie (string cookiename)
{
Httpcookie mycookie = new httpcookie (cookiename );
Datetime now = datetime. now;
Mycookie. expires = now. addyears (-2 );
Httpcontext. Current. response. Cookies. Add (mycookie );
}
}
}