Click here to Skip to main content
15,887,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, there is something I don't understand about cookies.when a cookie is created, it is created on the client's browser, I can see the cookie I created in the cookies on the browser.now 2 users logged in at different times by marking remember me, 2 cookies were created on the browser of both of them.when one of the users logs out, although I cannot physically delete the cookie, it says that I can only update the expried date. Let's say I did this, this is where I can't understand, when I log out, I select the cookie name there and remove it or reset the date,

but how does the server know which client this process belongs to? Because when I create it, I only give a generic cookie name, in short, a user-specific cookie_client_id is not created, when I delete the cookie, since I do this on the server, this cookie deletion will not occur for other users? In short, my question is how does it know that only x user's cookie should be deleted when a user logs out? thanks

What I have tried:

I tried to delete the cookie on the server but I failed.
Posted

The Server doesn't. The Client does.

Cookies are stored on the client machine by the browser under the current (Windows, Linux, Android or whatever) user - and since every user on a machine has separate data areas the browser stores it in the current user data (it doesn't have access to any other).

So when the server accesses cookies, it requests them from the browser, which accesses the current user data to fetch the right info.

Server users are different: they are a "construct" which uses the info from the browser cookies to decide which server user to log into your site. When that user logs out, they log out of your site, which doesn't affect cookies unless your site code specifically writes new cookie data.
 
Share this answer
 
I know that cookies are stored on the browser in the client's user profile, the server already writes it into a cookie on the browser. My question is that when the client wants to log out, it notifies the server, but this logout process takes place on the server side, for example 


<pre lang="C#">public ActionResult Logout()
        {
        
            Session.Clear();
            Session.Abandon();
            Session.RemoveAll();

            FormsAuthentication.SignOut();
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            return RedirectToAction("Index", "Login");
        }


Here it says FormsAuthentication.SignOut(); and sends a new cookie file to the client by resetting the date. Then can we say that the server recreates the cookie file and creates a new cookie file by updating the date and sends it to the browser, the browser overwrites the new cookie file with the existing one and prevents the cookie from working? If this is so, the browser does not send an old dated cookie to the server anyway by looking at the date and time of the cookie when making a request from the site, right? So on the server side, I don't need to check whether this cookie is old or not, if it is old, it never goes to the server anyway?

I don't understand the difference between a normal cookie and FormsAuthentication and again I don't understand the difference between creating a cookie with FormsAuthentication and FormsAuthentication.SetAutCookie

C#
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
        username,
        DateTime.Now,
        DateTime.Now.AddMinutes(30),
        isPersistent,
        userData,
        FormsAuthentication.FormsCookiePath)
;


C#
HttpCookie userInfo = new HttpCookie("userInfo");
userInfo["UserName"] = "Annathurai";
userInfo["UserColor"] = "Black";
userInfo.Expires.Add(new TimeSpan(0, 1, 0));
Response.Cookies.Add(userInfo);
 
Share this answer
 
Comments
Richard Deeming 31-Jan-24 5:23am    
You have already posted this comment as a comment; do not post a comment as a "solution" to your question!
Dave Kreskowiak 31-Jan-24 11:55am    
You don't "delete" the cookie. When the cookie expires, it's just not sent to the server anymore.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900