views:

69

answers:

1

The ASP.NET membership supports anonymous users and logged in users.

If you call FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); with a true for createPersistentCookie then the user will be logged in automatically when they revisit your site - even after closing the browser. If you don't enable this 'remember me' feature, then the anonymous cookie will still be around when the user visits your site again.

I'd like to do be able to store information in the user's anonymous profile when they are logged in. i.e. I don't want them to remain authenticated on the site if they go away and come back, but I'd still like be able to track certain things - like perhaps a visitCount property in the anonymous profile.

Is there any way to access a user's anonymous profile when they are authenticated. The two cookies exist so it should be possible. I don't want to reinvent half the wheel!

ps. I realize that tracking is skewed if multiple users use the system but thats fine.

+1  A: 

Sure. Simply check the AnonymousId property of the Request and built a ProfileBase.

var anonymousID = Request.AnonymousID;
if (anonymousID != null)
{
    var profile = ProfileBase.Create(anonymousID, false);

    int counter = (int)profile["counter"];
    profile["counter"] = ++counter;
    profile.Save();
}
Sky Sanders