During program development, cookies are rarely used, such as http://jb51.net/article/33590.htm. Used to be written as a Cookie set. What is called a Cookie set is a Cookie, which has multiple values. The following describes how to create and use a Cookie set.
Copy codeThe Code is as follows:
InsusBiz
Using System;
Using System. Web;
/// <Summary>
/// Summary description for InsusBiz
/// </Summary>
Public class InsusBiz
{
Private static HttpResponse Response
{
Get
{
Return HttpContext. Current. Response;
}
}
Private static HttpRequest Request
{
Get
{
Return HttpContext. Current. Request;
}
}
// Define a Cookie set
Private static HttpCookie InsusCookie
{
Get
{
Return Request. Cookies ["InsusCookie"] as HttpCookie;
}
Set
{
If (Request. Cookies ["InsusCookie"]! = Null)
{
Request. Cookies. Remove ("InsusCookie ");
}
Response. Cookies. Add (value );
}
}
// New Cookie set
Private static HttpCookie NewInsusCookie
{
Get
{
HttpCookie httpCookie = new HttpCookie ("InsusCookie ");
Return httpCookie;
}
}
// Remove Cookie set
Public static void RemoveInsusCookie ()
{
If (InsusCookie = null)
Response. Cookies. Remove ("InsusCookie ");
Else
Response. Cookies ["InsusCookie"]. Expires = DateTime. Now. AddDays (-1 );
}
// Create a Cookie to determine the user's logon status
Public static bool LoginOk
{
Get
{
Return InsusCookie = null? False: bool. Parse (InsusCookie. Values ["LoginOk"]);
}
Set
{
HttpCookie httpCookie = InsusCookie = null? NewInsusCookie: InsusCookie;
HttpCookie. Values ["LoginOk"] = value. ToString ();
InsusCookie = httpCookie;
}
}
// Create an account for the logged-on user for full-site use
Public static string MemberId
{
Get
{
Return InsusCookie = null? String. Empty: InsusCookie. Values ["MemberId"];
}
Set
{
HttpCookie httpCookie = InsusCookie = null? NewInsusCookie: InsusCookie;
HttpCookie. Values ["MemberId"] = value;
InsusCookie = httpCookie;
}
}
// If the Cookie used by the entire site can be written here, you can refer to the method of LoginOK or MemberId.
}
During the application, you will see attributes such as LoginOk, MemberId, and RemoveInsusCookie under the InsusBiz category:
How do I use these cookies in a program? For example, after successful login verification, you need to write the login status and login ID to the Cookie.
InsusBiz. LoginOk = true;
InsusBiz. MemberId = xxx;
This can be used to determine whether a user is logged on:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
If (! InsusBiz. LoginOk)
{
// You have not logged on
}
}
To retrieve the logon ID from any location:
Copy codeThe Code is as follows:
String memberId = InsusBiz. MemberId;
Finally, if you want to remove coke, you can use InsusBiz. RemoveInsusCookie, because it will change the Cookie expiration time to the past. This is usually applied to user Sign out events.