开发者

How to See List of Active Users Stored in Session Objects on the Admin Page

开发者 https://www.devze.com 2023-04-12 13:59 出处:网络
What i am doing is whenever users logs in I store his username in Session Object Now what i want on the Admin Page is the List of ACTIVE USERS (i.e No of Users which are 开发者_运维技巧presently wor

What i am doing is whenever users logs in I store his username in Session Object

Now what i want on the Admin Page is the List of ACTIVE USERS (i.e No of Users which are 开发者_运维技巧presently working with the Application (usernames in Session Objects)

Is there any way of doing that..

???

Thanks


Based on your comment to Davide Piras, if you are storing Session["user"] =username then you are only storing one element since you are always using the same key.

I would put everything in a List<string>, for example.

Something like this in your login page:

List<string> activeUsers = Cache["ActiveUsers"] as List<string>;
if(activeUsers==null)
   activeUsers = new List<string>();
activeUsers.Add(username_of_person_logged_in);
Cache["active_users"]=activeUsers;

Then in your "Admin" page...

List<string> activeUsers = Cache["ActiveUsers"] as List<string>;
if(activeUsers!=null)
{
    foreach(var item in activeUsers)
    {
      //do something with them
    }
}

Note: I changed it to Cache since Cache is shared across all users. Session will not work since it will be only valid on a per-user basis. Thanks to @CheckRaise for his comment.


The Session object cannot be accessed outside of its own session. If you need an administrator to be able to see all the active sessions, you need to use the Application object. For example, in global.asax:

protected void Application_Start(object sender, EventArgs e) {
  Application["Users"] = new List<string>;
}

Then, to add a user (possibly when they click 'Log in'):

Application.Lock();
((List<string>)Application["Users"]).Add(username);
Application.UnLock();

You should also remove the user in Session_End:

protected void Session_End(object sender, EventArgs e) {
  Application.Lock();
  ((List<string>)Application["Users"]).Remove(username);
  Application.UnLock();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号