Delete cookies in ASP. NET

Source: Internet
Author: User

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.

 
 
  1. if (Request.Cookies["UserSettings"] != null)  
  2. {  
  3. HttpCookie myCookie = new HttpCookie("UserSettings");  
  4. myCookie.Expires = DateTime.Now.AddDays(-1d);  
  5. Response.Cookies.Add(myCookie);  
  6. }   


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.

  1. Analysis of configuration files in ASP. NET
  2. . Net ria Services is as convenient as ASP. NET
  3. Overview of the UpdatePanel control in ASP. net ajax Extensions
  4. ASP. NET calls the UpdatePanel Update () method
  5. Introduction to the ASP. net ajax wcf Service

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.