Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3422034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:10:08+00:00 2026-05-18T06:10:08+00:00

I have been experimenting with code that will clear all of the cookies in

  • 0

I have been experimenting with code that will clear all of the cookies in an HttpContext.Response.

Initially, I used this:

DateTime cookieExpires = DateTime.Now.AddDays(-1);

for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
    HttpContext.Response.Cookies.Add(
        new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires });
}

However, this will error with an OutOfMemoryException because the for loop never exits – each time you add a cookie to the Response, it also gets added to the `Request.

The following approach works:

DateTime cookieExpires = DateTime.Now.AddDays(-1);

List<string> cookieNames = new List<string>();

for (int i = 0; i < HttpContext.Request.Cookies.Count; i++)
{
    cookieNames.Add(HttpContext.Request.Cookies[i].Name);
}

foreach (string cookieName in cookieNames)
{
    HttpContext.Response.Cookies.Add(
       new HttpCookie(cookieName, null) { Expires = cookieExpires });
}

So, what exactly is the relationship between HttpContext.Request.Cookies and HttpContext.Response.Cookies?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:10:08+00:00Added an answer on May 18, 2026 at 6:10 am

    Request.Cookies contains the complete set of cookies, both those that browser send to the server and those that you just created on the server.

    Response.Cookies contains the cookies that the server will send back.
    This collection starts out empty and should be changed to modify the browser’s cookies.

    The documentation states:

    ASP.NET includes two intrinsic cookie
    collections. The collection accessed
    through the Cookies collection of
    HttpRequest contains cookies
    transmitted by the client to the
    server in the Cookie header. The
    collection accessed through the
    Cookies collection of HttpResponse
    contains new cookies created on the
    server and transmitted to the client
    in the Set-Cookie header.

    After you add a cookie by using the
    HttpResponse.Cookies collection, the
    cookie is immediately available in the
    HttpRequest.Cookies collection, even
    if the response has not been sent to
    the client.


    Your first code sample should work if you make the for loop run backwards.
    The new cookies will be added after the end, so the backwards loop would ignore them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting with writing applications that use a local SQL Database to
I have been experimenting some problems with the fully distributed version. First of all
So I have been experimenting with building a new site... http://zergxost.com/test.html - it will
i've been experimenting with javascript's prototypal inheritance and have come across something that perhaps
I've been experimenting with an application that will scan an assembly, check for any
I have been experimenting with a jquery script that I found on the following
I have been experimenting with Firefox's Audio API to detecting silence in audio. (The
I have been experimenting with a lot of web development apps like Drupal, Moodle,
I have been experimenting with sending messages from two .NET Windows Forms applications using
I have been experimenting with TMask in Delphi 2010 and it seems to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.