Cookies during error handling

M

Marty McDonald

In global.asax we handle the application error event by gathering
exception info & putting it into a cookie. Then asp.net automatically
directs the user to our error page, where we use the cookie to show
descriptive error info. Our error page also deletes the cookie like
this...
//Remove cookie from response buffer, so deletion is effective server-
side.
if (context.Response.Cookies[cookieName] != null)
{
context.Response.Cookies.Remove(cookieName);
}

//Send an expired cookie to the client, to force deletion client-side.
if (context.Request.Cookies[cookieName] != null)
{
HttpCookie expiredCookie = new HttpCookie(cookieName);
expiredCookie.Expires = DateTime.Now.AddDays(-1d);
context.Response.Cookies.Add(expiredCookie);
}

Yet when the user navigates back to the app start page, the error
cookie is still present, which causes our app problems (if the error
cookie is present, our app skips certain logic). This is the code
that gets executed after the cookies are supposedly deleted as the
user is navigating away from the error page and to the app start
page...

public void Application_AuthenticateRequest(Object s, EventArgs e)
{
HttpCookie cookie = Request.Cookies["DiagnosticMessage"];
if (cookie != null) <====== The cookie is present here.
{
return;
}

Why would the cookie still be present? Thanks
 
M

Marty McDonald

In global.asax we handle the application error event by gathering
exception info & putting it into a cookie.  Then asp.net automatically
directs the user to our error page, where we use the cookie to show
descriptive error info.  Our error page also deletes the cookie like
this...
//Remove cookie from response buffer, so deletion is effective server-
side.
if (context.Response.Cookies[cookieName] != null)
{
    context.Response.Cookies.Remove(cookieName);

}

//Send an expired cookie to the client, to force deletion client-side.
if (context.Request.Cookies[cookieName] != null)
{
    HttpCookie expiredCookie = new HttpCookie(cookieName);
    expiredCookie.Expires = DateTime.Now.AddDays(-1d);
    context.Response.Cookies.Add(expiredCookie);

}

Yet when the user navigates back to the app start page, the error
cookie is still present, which causes our app problems (if the error
cookie is present, our app skips certain logic).  This is the code
that gets executed after the cookies are supposedly deleted as the
user is navigating away from the error page and to the app start
page...

public void Application_AuthenticateRequest(Object s, EventArgs e)
{
    HttpCookie cookie = Request.Cookies["DiagnosticMessage"];
    if (cookie != null)  <====== The cookie is present here.
    {
        return;
    }

Why would the cookie still be present?  Thanks

We found the cause. Our error page set the cookie's expiration to
"yesterday" as it should have. But then it did this...
Response.Cookies.Clear();
....which I believe clears the expired cookie before it ever gets a
chance to be sent to the client. So the only cookie on the client was
the non-expired one.
Our solution was to get rid of the Response.Cookies.Clear() line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top