Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Cookies Work in ASP.NET?

The website where I work is made up of several projects (written in several languages). Right now we have to use some awkward code in query strings and session variables to keep a person logged in when they go from project to project. Since cookies are domain specific we're trying to convert to them since they can be set in one project using one language yet be accessed by a different project (on the same domain) using a different language.

However I am having problems changing the value of a cookie and deleting them. Or to be more specific, I'm having trouble having any changes I make to a cookie stick.

For example in my logout code:

if (Request.Cookies["thisuserlogin"] != null)
{
    HttpCookie myCookie = new HttpCookie("thisuserlogin");
    myCookie.Value = String.Empty;
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
    Response.Cookies.Set(myCookie);
    litTest.Text = myCookie.Expires.ToString() + "<br />" + Request.Cookies["thisuserlogin"].Expires.ToString();
}

I wind up with one line being yesterday and the next line being 1/1/0001 12:00:00 even though they SHOULD be the same cookie. So why is it that even though the cookie was set, it's value did not change? Is there a way to force the user's computer to update a cookie's value, including deletion?

Thank you very much. PS Any URLs you can provide to give an easy-to-understand primer for cookies would be appreciated.

like image 889
Alverant Avatar asked Sep 04 '12 18:09

Alverant


People also ask

How cookies are activated using ASP?

Each time that ASP receives a request for a page, it checks the HTTP request header for a SessionID cookie. After storing the SessionID cookie in the user's browser, ASP reuses the same cookie to track the session, even if the user requests another . asp file, or requests an . asp file running in other application.

What is cookies in ASP.NET c#?

A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.

What is cookies and how it works?

A cookie is a small bit of information that a website stores on your computer. When you revisit the website, your browser sends the information back to the site. Usually a cookie is designed to remember and tell a website some useful information about you.

Where does ASP.NET store cookies?

We'll also see how to retrieve data from a cookie using ASP.NET. Cookies is a small piece of information stored on the client machine. This file is located on client machines "C:\Document and Settings\Currently_Login user\Cookie" path.


1 Answers

http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx

if (Request.Cookies["thisuserlogin"] != null)
{
    HttpCookie byeCookie = new HttpCookie("thisuserlogin");
    byeCookie.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Add(byeCookie);

    // Update Client
    Response.Redirect(Request.RawUrl);
}
like image 120
NickSuperb Avatar answered Sep 21 '22 12:09

NickSuperb