Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to delete cookies from the browser at log out, but when testing with the old session id .AspNet.ApplicationCookie. It still remains.
The code i have added to the log out is

C#
if (System.Web.HttpContext.Current != null)
            {
                HttpCookie aCookie;
                string cookieName;
                int limit = Request.Cookies.Count;
                for (int i = 0; i < limit; i++)
                {
                    cookieName = Request.Cookies[i].Name;
                    aCookie = new HttpCookie(cookieName);
                    aCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(aCookie);
                }
            }
            System.Web.HttpContext.Current.Session.Abandon();
            Response.Cookies.Clear();
            System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));


What I have tried:

I have tried setting the name to null
C#
<pre>cookieName = Request.Cookies[i].Name = null;


and setting the value to null
C#
<pre>aCookie.Value = null;
Posted
Updated 13-Aug-18 2:54am

You specify the new cookies with their settings and then you call Response.Cookies.Clear() so all of your amends are undone.

ASP.net will re-use the session id, if that's a problem because you are using the session id somewhere in your code then the solution is to change your code to not use the session id but to use something else.
 
Share this answer
 
It doesn't matter if i call
Response.Cookies.Clear()
at the start or the end. It has no effect.
if (System.Web.HttpContext.Current != null)
{
    System.Web.HttpContext.Current.Session.Clear();
    System.Web.HttpContext.Current.Session.Abandon();
    System.Web.HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
    HttpCookie aCookie;
    string cookieName;
    int limit = Request.Cookies.Count;
    for (int i = 0; i < limit; i++)
    {
        cookieName = Request.Cookies[i].Name;
        aCookie = new HttpCookie(cookieName);
        aCookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(aCookie);
    }
}


And by the session ID i guess you are talking about
ASP.NET_SessionId


I am trying to delete the <pre>.AspNet.ApplicationCookie
 
Share this answer
 
Comments
F-ES Sitecore 13-Aug-18 7:15am    
Cookies.Clear doesn't clear the cookies held by the browser, it empties the contents of the Cookies collection in your code. So you add a bunch of cookies to the collection then clear the collection, so when the response is sent to the client it won't include your expiry updates. Don't call .Clear at all
I have already tried leaving that out too. The same thing happens. The
AspNet.ApplicationCookie
session is still valid after setting the expiry to 1 day earlier and not calling the Clear at all.
 
Share this answer
 
This isn't going to work. This code runs entirely on the server-side. So, if the user actually clicks the "Logout" button, you've got a chance to overwrite the cookies.

The far bigger problem is users seldom ever click "Logout". They just close the browser. Your code will never be called and the cookies will not be cleared out or replaced.

So, what happens tomorrow when the user logs back in again? They'll have an entirely new session withe a different session ID, but the cookie session ID isn't going to match. Now you've got a huge problem.

NEVER save the session ID in a cookie on the client-side.
 
Share this answer
 
The cookie is only available for the session. Closing the browser down will close the session and therefore expire the cookies. If they login tomorrow, they will have log back in
So what is the resolution from a security stand point? If they logout or close the browser then they will have to login again, rather than an attacker high jacking the session.
 
Share this answer
 
Comments
Dave Kreskowiak 13-Aug-18 9:10am    
First, you posted this as an answer to your own question.

Next, closing the browser does NOT close the session. The session has to expire on the server before it's closed. The session "remaining time to live" is reset with every access from the web browser.

The browser also does not kill cookies on close by default. You have to enable this option in the browser settings. This is not typically done.
JakeFront 13-Aug-18 10:32am    
Yeah, sorry. i've been pressing the wrong button.
With regards to the cookie. This is all new to me about cookies and sessions. The security audit says we need to mitigate the possibility of reusing the old expired session ID in a new request. I've looked around and found the code above from a number of sites, but when this is put into the firefox, using cookie manager with the old session id, it doesn't redirect to the login page on a refresh.
I was hoping someone may have come across the same sort of issue.

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