get a list of online users in asp.net mvc

前端 未结 4 476
走了就别回头了
走了就别回头了 2020-12-09 00:46

I have a page in my application which always shows updated list of online users. Now, to keep the list-which is stored in application object- updated, i do the below steps

4条回答
  •  暖寄归人
    2020-12-09 01:38

    Do the following inside a global filter.

    public class TrackLoginsFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Dictionary loggedInUsers = SecurityHelper.GetLoggedInUsers();
    
            if (HttpContext.Current.User.Identity.IsAuthenticated )
            {
                if (loggedInUsers.ContainsKey(HttpContext.Current.User.Identity.Name))
                {
                    loggedInUsers[HttpContext.Current.User.Identity.Name] = System.DateTime.Now;
                }
                else
                {
                    loggedInUsers.Add(HttpContext.Current.User.Identity.Name, System.DateTime.Now);
                }
    
            }
    
            // remove users where time exceeds session timeout
            var keys = loggedInUsers.Where(u => DateTime.Now.Subtract(u.Value).Minutes >
                       HttpContext.Current.Session.Timeout).Select(u => u.Key);
            foreach (var key in keys)
            {
                loggedInUsers.Remove(key);
            }
    
        }
    }
    

    To retrieve the user list

    public static class SecurityHelper
    {
        public static Dictionary GetLoggedInUsers()
        {
            Dictionary loggedInUsers = new Dictionary();
    
            if (HttpContext.Current != null)
            {
                loggedInUsers = (Dictionary)HttpContext.Current.Application["loggedinusers"];
                if (loggedInUsers == null)
                {
                    loggedInUsers = new Dictionary();
                    HttpContext.Current.Application["loggedinusers"] = loggedInUsers;
                }
            }
            return loggedInUsers;
    
        }
    }
    

    Don't forget to Register you filter in global.asax. It's probably a good idea to have an app setting to switch this off.

    GlobalFilters.Filters.Add(new TrackLoginsFilter());
    

    Also remove users at logoff to be more accurate.

    SecurityHelper.GetLoggedInUsers().Remove(WebSecurity.CurrentUserName);
    

提交回复
热议问题