利用StateServer实现Session共享

57 篇文章 0 订阅

1、更改web.config文件system.web中修改节点

<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" cookieless="false"/>

注:tcpip=localhost:42424 tcpip的值可以设置为远程电脑的ip,如果设置为localhost说明session的值存放在本地服务器上面,如果设置为远程ip的话,则session存放在该远程服务器上。

在web.config文件system.web中添加节点

<machineKey validationKey="78AE3850338BFADCE59D8DDF58C9E4518E7510149C46142D7AAD7F1AD49D95D4" decryptionKey="5FC88DFC24EA123C" validation="SHA1"/>

在web.config文件<system.web>下添加节点

    <!-- 让其在跨站点访问时,使SESSION能共用 -->
    <httpCookies domain="test.com" />  

2、打开session所在服务器的“服务”设置,将“aspnet_state”服务开启,将其设置为自动启动模式

3、如果session所在的服务器不是本地服务器,需要在session所在服务器的注册器中将允许远程访问设置打开,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state \Parameters 节点 → 将 AllowRemoteConnection 的键值设置成“1”(1 为允许远程电脑的连接,0 代表禁止)→ 设置 Port (端口号)

4、修改Global.asax代码实现Session共享

方法一:在Global.asax代码中修改Session_Start()方法:

protected void Session_Start()
        {
            foreach (string moduleName in this.Modules)
            {
				//需要在web.config中配置
				//<add  key ="SessionKey" value="HrssSession" />
				//一定要保持不同应用程序中的appName的值一致
                string appName =ConfigurationManager.AppSettings["SessionKey"];
                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);
                        }
                    }
                }
            }


            if (Response.Cookies != null)
            {
                for (int i = 0; i < Response.Cookies.Count; i++)
                {
                    if (Response.Cookies[i].Name == "ASP.NET_SessionId")
                    {
                    	//ConfigurationManager.AppSettings["RootDomain"];
                        Response.Cookies[i].Domain = ".test.com";      //一定要保持不同应用程序的"ASP.NET_SessionId"的cookie的Domain值一致
                    }
                }
            }
        }

方法二:网站的Global.asax.cs中加入下面代码

public override void Init()
        {
            base.Init();
            foreach (string moduleName in this.Modules)
            {
				//需要在web.config中配置
				//<add  key ="SessionKey" value="HrssSession" />
				//一定要保持不同应用程序中的appName的值一致
                string appName =ConfigurationManager.AppSettings["SessionKey"];
                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);
                        }
                    }
                }
            }
        }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值