get a list of online users in asp.net mvc

前端 未结 4 479
走了就别回头了
走了就别回头了 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:24

    In your Account Controller

       public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (HttpRuntime.Cache["LoggedInUsers"] != null) //if the list exists, add this user to it
                    {
                        //get the list of logged in users from the cache
                        List loggedInUsers = (List)HttpRuntime.Cache["LoggedInUsers"];
                        //add this user to the list
                        loggedInUsers.Add(model.UserName);
                        //add the list back into the cache
                        HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
                    }
                    else //the list does not exist so create it
                    {
                        //create a new list
                        List loggedInUsers = new List();
                        //add this user to the list
                        loggedInUsers.Add(model.UserName);
                        //add the list into the cache
                        HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
                    }
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
    
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
    
            // If we got this far, something failed, redisplay form
            return View(model);
        }
    
    
        public ActionResult LogOff()
        {
            string username = User.Identity.Name; //get the users username who is logged in
            if (HttpRuntime.Cache["LoggedInUsers"] != null)//check if the list has been created
            {
                //the list is not null so we retrieve it from the cache
                List loggedInUsers = (List)HttpRuntime.Cache["LoggedInUsers"];
                if (loggedInUsers.Contains(username))//if the user is in the list
                {
                    //then remove them
                    loggedInUsers.Remove(username);
                }
                // else do nothing
            }
            //else do nothing
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
    

    in your partial view.

    @if (HttpRuntime.Cache["LoggedInUsers"] != null)
    {
        List LoggedOnUsers = (List)HttpRuntime.Cache["LoggedInUsers"];
        if (LoggedOnUsers.Count > 0)
        {
        
      @foreach (string user in LoggedOnUsers) {
    • @Html.Encode(user)
    • }
    } }

    render this partial view when user log in.

    use this script call ever 90 second

    
    

提交回复
热议问题