Use a Cookie set in a program (define/create/delete) and Use Cases

Source: Internet
Author: User

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.

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.