Session集中式管理

      Asp.net Session集中式管理主要有StateServer(状态服务器)、Sqlserver(数据库服务器)、自定义(如Redis缓存服务器)等,本文主要介绍StateServer模式和自定义模式(Redis缓存)的使用方法
     集中式管理Session主要应用在集群服务器,所以首先要模拟集群服务器,方法在同一个网站复制多份在IIS多个端口部署,然后是使用ngix模拟负载均衡效果,随机访问其中一台服务器,参考文章:http://www.cnblogs.com/yanweidie/p/4658136.html
 
 
一、StateServer
       数据库存储在单独一台机器的内存缓冲区中,使用Asp.net 状态服务来控制这个缓冲区,机器重启后数据会被清空,故适合于非持久化的数据。设置步骤如下:
       1、在web.config中设置
           <!--端口默认为:42424  过期时间为:20分钟-->
         <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" timeout="20" />
       2、开启远程服务器的StateSever功能
            编辑HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\aspnet_state\Parameters注册表项中的Port值和AllowRemoteConnection修改成1
       3、运行services.msc启动Asp.net 状态服务
       4、2,3步可使用批处理命名一起执行  (1:表示设置为允许远程访问,0:表示只允许本机、42424位默认端口,上线项目建议修改此端口提高安全性)
              
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\aspnet_state\Parameters" /v "AllowRemoteConnection" /t REG_DWORD  /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\aspnet_state\Parameters" /v "Port" /t REG_DWORD  /d 42424 /f

net stop aspnet_state
net start aspnet_state

pause

 

       5、参考文档
http://m.blog.csdn.net/blog/ahywg/39232809
      6、问题
           1、提高安全性
                a) 修改默认端口,参考上面的批处理命令
                b) 设置machineKey,参考http://www.cnblogs.com/ruiati/archive/2013/08/16/3262061.html
                  <machineKey decryptionKey="FD69B2EB9A11E3063518F1932E314E4AA1577BF0B824F369" validationKey="5F32295C31223A362286DD5777916FCD0FD2A8EF882783FD3E29AB1FCDFE931F8FA45A8E468B7A40269E50A748778CBB8DB2262D44A86BBCEA96DCA46CBC05C3" validation="SHA1" decryption="Auto"/>
     <sessionState cookieless="false" timeout="50" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"/>
 
2、共享session问题(多个服务器时Session不能共享,如A服务器设置Session["UserId"]=123,B服务器读取Session["UserId"]时返回null)
                  可能是域的问题,解决方案是在global.asax的Init方法加下述代码
 public override void Init()
        {
            base.Init();
            foreach (string moduleName in this.Modules)
            {
                string appName = "MYAPP";  //可任意命名
                IHttpModule module = this.Modules[moduleName];
                SessionStateModule ssm = module as SessionStateModule;
                if (ssm != null)
                {
                    FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", BindingFlags.Instance | BindingFlags.NonPublic);
                    SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
                    if (store == null)//In IIS7 Integrated mode, module.Init() is called later
                    {
                        FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
                        HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
                        FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
                        appNameInfo.SetValue(theRuntime, appName);
                    }
                    else
                    {
                        Type storeType = store.GetType();
                        if (storeType.Name.Equals("OutOfProcSessionStateStore"))
                        {
                            FieldInfo uribaseInfo = storeType.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
                            uribaseInfo.SetValue(storeType, appName);
                        }
                    }
                }
            }
        }   

 

            
posted @ 2015-10-09 15:16    阅读(861)  评论(0编辑  收藏  举报