静态会话类和多用户 [英] Static Session Class and Multiple Users

查看:79
本文介绍了静态会话类和多用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个类来存储用户ID和用户角色的会话。我不知道如何当多个用户在同一时间在网站上这个类的行为。没有人看到有问题呢?

 公共静态类SessionHandler
    {
        // ***会话字符串值***********************        私人静态字符串_userID =用户名;
        私人静态字符串_userRole =UserRole的;        // ***集和获取****************************************** ****************        公共静态字符串用户名
        {
            得到
            {
                如果(HttpContext.Current.Session [SessionHandler._userID] == NULL)
                {返回的String.Empty; }
                其他
                {返回HttpContext.Current.Session [SessionHandler._userID]的ToString(); }
            }
            组
            {HttpContext.Current.Session [SessionHandler._userID] =价值; }        }
        公共静态字符串的UserRole
        {
            得到
            {
                如果(HttpContext.Current.Session [SessionHandler._userRole] == NULL)
                {返回的String.Empty; }
                其他
                {返回HttpContext.Current.Session [SessionHandler._userRole]的ToString(); }
            }
            组
            {HttpContext.Current.Session [SessionHandler._userRole] =价值; }
        }}


解决方案

在公布的code是一些code,我们在这里的翻版。

据现在工作正常2年。

每个用户访问自己的会话。向服务器发出的每个请求是一个新的线程。即使2请求是同时,所述HttpContext.Current是为每个请求的不同

I am building a class to store User ID and User Role in a session. I'm not sure how this class will behave when multiple users are on the site at the same time. Does anyone see a problem with this?

public static class SessionHandler
    {   
        //*** Session String Values ***********************

        private static string _userID = "UserID";
        private static string _userRole = "UserRole";

        //*** Sets and Gets **********************************************************

        public static string UserID
        {
            get
            {
                if (HttpContext.Current.Session[SessionHandler._userID] == null)
                { return string.Empty; }
                else
                { return HttpContext.Current.Session[SessionHandler._userID].ToString(); }
            }
            set
            { HttpContext.Current.Session[SessionHandler._userID] = value; }

        }
        public static string UserRole
        {
            get
            {
                if (HttpContext.Current.Session[SessionHandler._userRole] == null)
                { return string.Empty; }
                else
                { return HttpContext.Current.Session[SessionHandler._userRole].ToString(); }
            }
            set
            { HttpContext.Current.Session[SessionHandler._userRole] = value; }
        }

}

解决方案

The code you posted is the exact replica of some code we have here.

It has been working fine for 2 years now.

Each users access is own session. Every request made to the server is a new thread. Even though 2 request are simultaneous, the HttpContext.Current is different for each of those request.

这篇关于静态会话类和多用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆