SessionManager实例对其他用户不是唯一的?

时间:2016-08-04 10:53:45

标签: c# session static

我为维护会话值创建了SessionManager类。当多个用户登录系统时,问题是用户自动更改Loge。

下面我提到我的代码。

public  class SessionManager
    {
        //private readonly Dictionary<Guid, SessionInfo> __session;
        private ISecurityService _service;
        private static HttpSessionState _session=null;
        private static SessionInfo _sessionInfo { get; set; }
        private static SessionManager instance ;

        private SessionManager()
        {
            _session = HttpContext.Current.Session;
        }

        public static SessionManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SessionManager();
                }
                return instance;
            }
        }

        public void CreateSession(ISecurityService service)
        {
            if (IsNull())
            {
                _service = service;
                Guid sid = Guid.NewGuid();
                SessionInfo info = new SessionInfo();
                info.SessionID = sid;
                info.UserId = _service.UserID;
                info.ApplicationID = _service.ApplicationID;

                info.UserName = _service.UserName;
                info.Name = _service.Name;
                info.Role = _service.Role;
                info.RoleInPermission = _service.RoleInPermission;    
                info.ClientId = service.ClientId;
                info.BranchId = service.BranchId;
                _session["_zcargo"] = info;
            }
        }

        public bool IsAuthenticated()
        {
            return !IsNull();
        }

        public SessionInfo GetSession()
        {
            if (!IsNull())
            {
                _sessionInfo = GetSessionInfo();
                return _sessionInfo;
            }
            return null;
        }

        public Guid GetSessionID()
        {
            if (!IsNull())
            {
                _sessionInfo = GetSessionInfo();
                return _sessionInfo.SessionID;
            }

            return new Guid("");
        }

        private SessionInfo GetSessionInfo()
        {
            if (!IsNull())
            {
                return (SessionInfo)_session["_zcargo"];
            }
            return null;
        }

        public bool IsNull()
        {
            return _session["_zcargo"] == null;
        }

        public  void RemoveSession()
        {
            _session["_zcargo"] = null;
            _session.Remove("_zcargo");
        }

        public void AbandonSessions()
        {
            _session.Abandon();
        }
    }

以下代码使用会话信息。

SessionManager.Instance.GetSession().ClientId

0 个答案:

没有答案