How can I get all logged-in users on my website?

2019-09-07 05:49发布

I need all active users logged on my website. I know I can take my logged-in user:

var currentUser = manager.FindById(User.Identity.GetUserId());

but how can I take all?

2条回答
疯言疯语
2楼-- · 2019-09-07 06:13

Create a field in user table named "IsLogin". Set IsLogin = 0 when user Signup. Update the field whenever user logged in to the system i.e. IsLogin = 1

Now in order to get all logged-in users : Check all records in a user table whose have IsLogin = 1 Your choice save the result in user object or just store ids or names of those users who are logged-in Hence you store the result in array or any collection (i.e List) Now you can easily enumerate list from start till end to get all logged-in users.

I used this method, It is the most recommended way.

查看更多
我只想做你的唯一
3楼-- · 2019-09-07 06:16

ASP.NET membership has a feature that works for FormsAuthentication. Otherwise you need to write to a persistent store somewhere the most recent activity date & then query that table for all users active within a certain window. A web app is stateless, so "actively logged" in is a fictional abstraction we imagine over a stateless protocol. After a request is over the server has forgotten about you. Another hack might be to count how many sessions are still alive if you are using sql session store.

查看更多
登录 后发表回答