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 930695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:22:45+00:00 2026-05-15T20:22:45+00:00

I’m creating an HttpCookie, setting only the name and value and not the expires

  • 0

I’m creating an HttpCookie, setting only the name and value and not the expires property, then adding it to the response. Simple enough. The cookie is created (but not persisted) as expected. The problem is when the session changes for some reason (like the website was rebuilt, or I rebuilt my app when debugging) then the cookie stays around. I want the cookie to be valid for only the original session it was created on.

According to MSDN it says: “If you do not specify an expiration limit for the cookie, the cookie is not persisted to the client computer and it expires when the user session expires.”

I guess I don’t know exactly what “session expires” encompasses. I figure the cookie gets deleted after 20 min when the session expires. But should the cookie get deleted if the session it was created on doesn’t exist anymore for any number of reasons? The only time I’ve seen the cookie get deleted is when the user closes all browser windows and opens a new one.

If this is all true, I may have to store the original session id (“ASP.NET_SessionId”) in the cookie, then check it against the current session id, if they’re different, then delete the cookie or create a new one.

Here’s the code (the only difference between my cookie and the one in the MSDN examples is I’m storing multiple values in the cookie):

private void SaveValuesToCookie(string[] names, string[] values)
{
    HttpCookie cookie = new HttpCookie("MyCookie");

    for (int i = 0; i < names.Length; i++)
    {
        string name = names[i];
        cookie.Values[name] = values[i];
    }
    Response.Cookies.Add(cookie);
}

private string GetValueFromCookie(string name)
{
    HttpCookie cookie = Request.Cookies["MyCookie"];
    if (cookie == null)
        return null;

    return cookie.Values[name];
}
  • 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-15T20:22:45+00:00Added an answer on May 15, 2026 at 8:22 pm

    The only time I’ve seen the cookie get
    deleted is when the user closes all
    browser windows and opens a new one.

    And that is exactly what MSDN means when it says the cookie will be deleted when the session expires. Unfortunately, I believe this isn’t consistant across browsers anyway, so it’s not much use to anyone.

    You should always set an expiry date on Cookies.

    If this is all true, I may have to
    store the original session id
    (“ASP.NET_SessionId”) in the cookie,
    then check it against the current
    session id, if they’re different, then
    delete the cookie or create a new one.

    I hate to say it but this isn’t going to help you either. The .NET Framework likes to recycle session IDs, so you can’t guarantee it will be different.

    Bad news out of the way, I would advise you to reconsider what you’re trying to do from an architectural standpoint.

    Restarting the app is something that happens entirely on the server; cookies are something that happen entirely on the client. While the client will talk to the server, it is purely a Request/Response relationship, the server cannot communicate events such as an application restart to the browser.

    If you want to store a value somewhere which is only valid for the lifespan of a server session, why not store it in Session rather than in a Cookie?

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

Sidebar

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.