14

I'm getting started with ASP.NET Identity, but I've been unable to find a way to fetch a list of the current logged in users. To get all users I can just user

new ApplicationDbContext().Users

where ApplicationDbContext comes with the ASP.NET Web Application template, and extends IdentityDbContext.

But the IdentityUser class does not seem to have any way to query for the logged in status.

Thank you for any pointers.

SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65
  • 3
    I'll be impressed if this is currently implemented in ASP.NET Identity – Jonesopolis Apr 10 '14 at 18:14
  • Have you thought about capturing an event that occurs when a user logs in? Store that information in a database table or in memory. When the user logs out, remove that user from the database or memory. – mason Apr 10 '14 at 18:16
  • I don't believe so - concept of "currently logged in" is very application specific so it is unlikely to have implementation that work for many cases. – Alexei Levenkov Apr 10 '14 at 18:16
  • What do you mean it's application specific? Also, about capturing the log in event. I could do it, but what about when a user is automatically logged out? I haven't seen any events related to log in or log out. – SoManyGoblins Apr 10 '14 at 18:48
  • 1
    you can create custom `IHttpModule` implementation, there you can subscribe to PostAuthenticateRequest and AuthenticateRequest context events, and check there if context.Request.IsAuthenticated, this will be a good starting point. – Andrew Apr 10 '14 at 19:21

1 Answers1

23

There is no built in support for this. One simple way you could implement this would be to add a LastActivityDate to your user, which is updated whenever a user does something on your site. Then you can just query against that looking for users who are active within some reasonable time, like 30 minutes and display that.

Hao Kung
  • 28,040
  • 6
  • 84
  • 93