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

廉价感情. 提交于 2020-01-06 06:51:07

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/26550014/how-can-i-get-all-logged-in-users-on-my-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!