如何为用户创建唯一会话:ASP.NET

时间:2018-04-09 05:47:42

标签: asp.net-mvc session login

我一直在学习ASP.NET,并且我已经意识到我的Web应用程序只为所有用户创建了一个静态会话,即如果一个用户注销了所有用户,有时候会话就是甚至交换(假设userA登录,在userB登录后,当userA刷新时他正在看到userB的数据)。

我的SessionManager类如下

SessionManager.cs

public  class SessionManager
    {
        #region Private Data

        private static String USER_KEY = "user";

        #endregion

        public static Employee CurrentUser
        {
            get;
            set;
        }
        public static string UserType
        {
            get;
            set;
        }
        public static Int32 SessionTimeout
        {
            get
            {
                return System.Web.HttpContext.Current.Session.Timeout;
            }
        }

        public static String GetUserFullName()
        {
            if (SessionManager.CurrentUser != null)
                return SessionManager.CurrentUser.FirstName;
            else
                return null;
        }
        public static Boolean IsUserLoggedIn
        {
            get
            {
                if (SessionManager.CurrentUser != null)
                    return true;
                else
                    return false;
            }

        }

        #region Methods
        public static void AbandonSession()
        {
            for (int i = 0; i < System.Web.HttpContext.Current.Session.Count; i++)
            {
                System.Web.HttpContext.Current.Session[i] = null;
            }
            System.Web.HttpContext.Current.Session.Abandon();
        }

        #endregion
    }

登录控制器:

 [HttpPost]
        public ActionResult Index(String txtUserName, String txtPassword)
            if (User.Identity.IsAuthenticated)
            {
                return View();
            }
            else
            {
                if (ModelState.IsValid)
                {
Employee obj = (from o in db.Employees
                                        where o.Email == txtUserName && o.Password == txtPassword
                                        select o).FirstOrDefault();
 if (obj != null)
                        {
  var dh = db.Departments.Where(x => x.LeadBy == obj.EmployeeId).FirstOrDefault();
                            var tl = db.Teams.Where(x => x.LeadBy == obj.EmployeeId).FirstOrDefault();
                            if (dh == null && tl == null)
                            {
                                Session["UserType"] = "EMP";
                            }
                            else if (dh != null && tl != null)
                            {
                                Session["UserType"] = "DH&TL";
                            }
                            else if (dh != null)
                            {
                                Session["UserType"] = "DH";
                            }
                            else if (tl != null)
                            {
                                Session["UserType"] = "TL";
                            }
 SessionManager.CurrentUser = obj; //how can I create different obj for different users here?
 var currentEnrollID = SessionManager.CurrentUser.EnrollNumber;      
 var currentEmployeeID = SessionManager.CurrentUser.EmployeeId;
 var currentEmpName = SessionManager.CurrentUser.FirstName + " " + SessionManager.CurrentUser.LastName;

我在整个应用程序中一直使用这样的会话,因此修改更改会有不同的方法。

public ActionResult Logout()
        {
            if (SessionManager.IsUserLoggedIn)
            {
                SessionManager.CurrentUser.EmployeeId = 0;
                SessionManager.AbandonSession();
                Session.Clear();
                Session.Abandon();
                Session.RemoveAll();
            }
            return RedirectToAction("Index","Login");
        }

1 个答案:

答案 0 :(得分:0)

这与ASP.NET无关,但更多的是静态成员的工作方式。

真正的问题是你的SessionsManager,它包含每次用户登录时存储值的静态方法。这意味着在应用程序的不同会话中共享相同的实例。

我有一个更新的SessionManager,你可以在下面看到。我已将SessionManager对象存储在会话对象中,以便会话处于活动状态。当您使用 SessionManager.Current 调用它时,它将按会话返回相同的实例。

public class SessionManager {
    #region Private Data

    private static String USER_KEY = "user";

    #endregion

    public static SessionManager Current {
        get{
            if (HttpContext.Current.Session[USER_KEY] != null) {
                return (SessionManager) HttpContext.Current.Session[USER_KEY];
            } else {
                var sess = new SessionManager ();
                HttpContext.Current.Session[USER_KEY] = sess;
                return sess;
            }
        }
    }

    public Employee CurrentUser {
        get;
        set;
    }
    public string UserType {
        get;
        set;
    }
    public Int32 SessionTimeout {
        get {
            return System.Web.HttpContext.Current.Session.Timeout;
        }
    }

    public String GetUserFullName () {
        if (SessionManager.Current.CurrentUser != null)
            return SessionManager.Current.CurrentUser.FirstName;
        else
            return null;
    }
    public Boolean IsUserLoggedIn {
        get {
            if (SessionManager.Current.CurrentUser != null)
                return true;
            else
                return false;
        }

    }

    #region Methods
    public void AbandonSession () {
        for (int i = 0; i < System.Web.HttpContext.Current.Session.Count; i++) {
            System.Web.HttpContext.Current.Session[i] = null;
        }
        System.Web.HttpContext.Current.Session.Abandon ();
    }

    #endregion
}