asp.net网站不同子域名共享session信息

1session信息可序列化 [Serializable]

 [Serializable]
    public class UserSession
    {  

        public string GroupCode
        {
            get;
            set;
        }
      
        public string RoleCode
        {
            get;
            set;
        }
	 }

2 使cookie的path为根域名而不是子域名

function setCookie(name, value) {//两个参数,一个是cookie的名子,一个是值
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString()+"; path=/"; 
}

3开启服务aspnet_state.exe


4修改web.config的sessionState

    <!--<sessionState mode="InProc" cookieless="false" timeout="600" />-->
    
    <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" timeout="60"></sessionState>

5修改Global.asax文件

在 void Session_Start(object sender, EventArgs e)中添加调用如下代码


 private void InitSession()
    {
        foreach (string moduleName in this.Modules)
        {
            string appName = "APPNAME";
            IHttpModule module = this.Modules[moduleName];
            SessionStateModule ssm = module as SessionStateModule;
            if (ssm != null)
            {
                System.Reflection.FieldInfo storeInfo = typeof(SessionStateModule).GetField("_store", System.Reflection.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")
                {
                    Response.Cookies[i].Domain = ".dhcc.com.cn";      //一定要保持不同应用程序的"ASP.NET_SessionId"的cookie的Domain值一致
                }
            }
        }
    }


   void Session_Start(object sender, EventArgs e)
    {
	  InitSession();
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值