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

来源:互联网 发布:淘宝奇葩买家秀大尺度 编辑:程序博客网 时间:2024/05/17 04:32

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();}



原创粉丝点击