在不同的二级域名下Session共享的问题解决

首先看看webconfig的配置

分两种情况,具体配置看下面的

web.config配置如下:


IIS6:


   

  <system.web>

    <sessionState mode="StateServer" stateConnectionString="tcpip=192.168.1.60:42424" timeout="30" />

    <httpModules>
      <add name="SessionSharedHttpModule" type="Myun.Web.SessionSharedHttpModule, Myun.Web" />
    </httpModules>

  </system.web>



  </system.web>


IIS7:托管管道模式为 集成管道模式,否则与IIS6相同


 
 <system.web>


    <sessionState mode="StateServer" stateConnectionString="tcpip=192.168.1.60:42424" timeout="30" />


  </system.web>


 <system.webServer>


    <modules>
      <add name="SessionSharedHttpModule" type="Myun.Web.SessionSharedHttpModule, Myun.Web" />
    </modules>


 </system.webServer>


接下来后台代码的处理:

代码如下:SessionSharedHttpModule.cs


//****************************//

//create date:2010-01-11

//****************************//

namespace YeWenBin.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.SessionState;
    using System.Reflection;
    /// <summary>
    /// 二级域名会话共享 继承IHttpModule
    /// </summary>
    public class SessionSharedHttpModule : IHttpModule
    {
        string _rootDomain = null; //一级域名


        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            _rootDomain = "yewenbin.cn"; //一级域名赋值

            //去除一级域名以外信息(将www.yewenbin.cn改为yewenbin.cn,如果一级域名不是常量赋值的话)
            //_rootDomain = _rootDomain.Substring(_rootDomain.LastIndexOf('.', _rootDomain.LastIndexOf('.') - 1) + 1);
            
            //要实现会话共享还得修改OutOfProcSessionStateStore类下的一个私有的静态字段s_uribase
            //OutOfProcSessionStateStore的声明为:
            //internal sealed class OutOfProcSessionStateStore : SessionStateStoreProviderBase
            //s_uribase的声明为:
            //static string       s_uribase;
            //关于OutOfProcSessionStateStore类以及s_uribase字段的内容请查阅OutOfProcStateClientManager.cs文件
            //文件路径:Framework源代码\V2.0.5727\dd\ndp\fx\src\xsp\System\Web\State\OutOfProcStateClientManager.cs
            Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
            FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
            if (uriField == null)
                throw new ArgumentException("UriField was not found");

            uriField.SetValue(null, _rootDomain);
            context.EndRequest += new EventHandler(context_EndRequest);   

        }

        /// <summary>
        /// 从发送给客户端的Cookie集合中找出记录会话ID的Cookie
        /// 并修改它的Domain属性值为要共享的一级域名
        /// </summary>

        void context_EndRequest(object sender, System.EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            for (int i = app.Context.Response.Cookies.Count - 1; i >= 0; i--)
            {

                //ASP.NET_SessionId是默认的存储会话ID的key,如果修改了默认值这里要修改成一致的
                if (app.Context.Response.Cookies[i].Name.Equals("ASP.NET_SessionId"))
                {
                    app.Context.Response.Cookies[i].Domain = _rootDomain;
                    return;
                }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值