The Cookie in the user's computer cannot be directly deleted in ASP. NET. However, you can set the Cookie expiration date to the previous date and let your browser Delete the Cookie. When a user sends a request to the page in the domain or path where the Cookie is set, the browser determines that the Cookie has expired and removes it.
You can call the Remove Method of the Cookie set to delete the Cookie from the set on the server side so that the Cookie will not be sent to the client. However, if the client already has a Cookie
Allocate the past expiration date to the Cookie
1. Check whether the Cookie exists. If so, create a new Cookie with the same name.
2. Set the Cookie expiration date to a time in the past.
3. Add the Cookie to the Cookie set object.
The following code example shows how to set an expiration date for a Cookie.
- if (Request.Cookies["UserSettings"] != null)
- {
- HttpCookie myCookie = new HttpCookie("UserSettings");
- myCookie.Expires = DateTime.Now.AddDays(-1d);
- Response.Cookies.Add(myCookie);
- }
Compile code
This example requires:
1. An ASP. NET webpage.
2. A previously written Cookie named UserSettings,
Reliable Programming
For security reasons, you can only read the cookies set for pages in the same domain. If the Path attribute of the Cookie has been set, the Cookie can only be used for pages and subfolders in the domain Path.
When reading a specific Cookie value, test whether the Cookie exists and whether it has a value. Otherwise, an exception occurs. The preceding example shows how to delete a Cookie in ASP. NET.
- Analysis of configuration files in ASP. NET
- . Net ria Services is as convenient as ASP. NET
- Overview of the UpdatePanel control in ASP. net ajax Extensions
- ASP. NET calls the UpdatePanel Update () method
- Introduction to the ASP. net ajax wcf Service