views:

89

answers:

3

Is it best to check for a null User when trying to access UserID or should I assign an anonymous account? Or other?

+2  A: 

You should check to see if there is no current user, and then create a new user account automatically and log them in, behind the scenes. If you use cookies (and assuming the user has them enabled), subsequent visits by this "anonymous user" will still map to the same account in your user repository, which is useful for tracking activity.

You can automatically put users into "registered" and "automatic/anonymous" groups. This makes it easy for your code to discern how to behave - for example, deciding whether to show "Log in" or "My Account" links by whether the current user is a member of the "anonymous" group. It also makes it easier to migrate over a history of activity to a registered account if/when the user decides to do so.

Rex M
Do you mean the anonymous user here to be different from the 'visitor' that just browsing around? (ie like stack overflow unregistered) Or do you mean that any person visiting the site should be signed in behind the scenes? isnt that too much?
zsharp
@zsharp any person visiting the site should be signed in behind the scenes. That might sound like a lot at first, but it actually simplifies things because all users are treated the same - as a logged-in user. The only difference is the login mechanism, which can be abstracted into two providers - a username/password login and an automated, invisible login.
Rex M
one other thing, would i create a new member and assign a new id or reuse a 'guest' account? if the former, would i need to clean up the db periodically of anonymous users?
zsharp
@zsharp definitely create a new account when a guest cannot be identified by cookie or what-have-you. If you start hitting space constraints because your database is so full of guest accounts, that's a good problem to have!
Rex M
+3  A: 

In ASP.NET membership you can call the following function to check whether or not a user is logged in / authenticated / anonymous or not...

User.Identity.IsAuthenticated()
Ropstah
A: 

If you are on a page, the User.Identity.IsAuthenticated() property will give you a boolean value. If you are not on a page (ie, want to check in a class method), you need to import the System.Web.HttpContext namespace.

Jason
was i really voted down for this? really?
Jason