Copy codeThe 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 );
}
}
}