2

Is there an API to clear anonymous cookie?

Igor Golodnitsky
  • 4,456
  • 7
  • 44
  • 67

4 Answers4

4

Yes!

 System.Web.Security.AnonymousIdentificationModule.ClearAnonymousIdentifier()

See this useful article : (Migrating Anonymous User Settings)

Also Best way to Migrate Anonymous Profile stackoverflow question.

Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • note: as far as i can tell the two can coexist just fine - so you may not want to remove it. don't just assume that you need to remove it just because you've logged someone in. you can even link the two profiles on the back end - nobody can tell this from client side but it may be useful to track users. – Simon_Weaver Dec 08 '09 at 23:24
  • update: I've changed my mind on this somewhat. it seems that Profile_MigrateAnonymous fires not just when a user logs in - but in the presence of both an anonymous and authenticated cookie. my other question may be of interest http://stackoverflow.com/questions/1895266/can-anonymous-and-authenticated-profiles-coexist-together-in-asp-net – Simon_Weaver Dec 13 '09 at 01:15
0

I'm using MVC4, and in my scenario I want to be able to delete the anonymous cookie so that a new one is auto-generated even if a user never logs in. (So that testers don't have to delete their cookies every time they want to come back to the site as a new user.) AnonymousIdentificationModule.ClearAnonymousIdentifier doesn't work in this scenario because you can only call that if the user is authenticated. (Seems like an optional parameter to that function would be nice.) So alternatively you must rewrite the anonymous cookie with an expiration date in the past. I did this by making a basic Action Method that an anonymous user can go to whenever they want to and restart as a fresh anonymous user:

    [HttpGet]
    [AllowAnonymous]
    public ActionResult ClearMyCookie()
    {
        HttpCookie cookie = new HttpCookie(".ASPXANONYMOUS");
        cookie.Expires = DateTime.Now.AddDays(-10);
        Response.Cookies.Add(cookie);
        // Make sure to also do Session.Clear() if you're using session.
        return RedirectToAction("index", "home");
    }

Change ".ASPXANONYMOUS" to your cookie's name. I'm using the default anonymous name above.

TTT
  • 22,611
  • 8
  • 63
  • 69
0

Initially i was facing an issue with "AnonymousIdentificationModule.ClearAnonymousIdentifier()".Error was "ClearAnonymousIdentifier is not supported when the feature is disabled or the user is anonymous."

This issue only occur when try to clear the Anonymous identifier when the user itsef is an anonymous user. Means an anonymous user cannot clear cookie of other anonymous user.

For doing this, place code in the case when user is authenticated (logged in user) like :

if(Request.IsAuthenticated)
{
   AnonymousIdentificationModule.ClearAnonymousIdentifier();
}

This will do the trick.

Sachin Kumar
  • 974
  • 2
  • 13
  • 23
-1

I'm not sure if this is what you are asking, but you can clear all cookies sent to browser as follows:

Response.Cookies.Clear();

If you are having authentication problems, you may want to clarify the question.

HectorMac
  • 6,073
  • 4
  • 24
  • 24