• asp.net基于StateServer的二级域名共享session


    备注:亲自试验有效,如果网友有通过下面的教程未实现session共享的,欢迎留言说明你遇到的问题。必有回复。

    最近为实现的二级域名共享session纠结好久。网上的很多实现的方法试了都不行,查了很久才找到了实现的方法。在这里记录下,以备以后所需。

    二级域名共享session有多种实现方法:第一种依赖于cookie。第二种直接共享session。

    我这里分享的是第二种直接共享session。session的存储方法有三种 InProc, StateServer, SQLServer。

    这里分享的基于StateServer的存储方试。 

    实现的思路:让主域名和二级域名的ASP.NET_SessionId 保持一致。

    实现的步骤:

    第1步.把sessionState的存储方式改为:StateServer,默认是InProc。 tcpip=127.0.0.1:42424  这段是指本机的意思。我试验的是主域名和二级域名都在都一台服务器上。如下图红色框框区域:

    第2步.在system.web结点下添加结点 <httpCookies domain=".test.com"/> ,这里test.com改为你自己的域名。这个结点的功能就是实现ASP.NET_SessionId 保持一致。

    查看ASP.NET_SessionId的方法可以用google浏览器打开网页,然后F12,如下面板查看ASP.NET_SessionId

    第3步在Global.asax中添加如下代码。

       public override void Init()
        {
            base.Init();
            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 | System.Reflection.BindingFlags.NonPublic);
                    SessionStateStoreProviderBase store = (SessionStateStoreProviderBase)storeInfo.GetValue(ssm);
                    if (store == null)//In IIS7 Integrated mode, module.Init() is called later
                    {
                        System.Reflection.FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
                        HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
                        System.Reflection.FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                        appNameInfo.SetValue(theRuntime, appName);
                    }
                    else
                    {
                        Type storeType = store.GetType();
                        if (storeType.Name.Equals("OutOfProcSessionStateStore"))
                        {
                            System.Reflection.FieldInfo uribaseInfo = storeType.GetField("s_uribase", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
                            uribaseInfo.SetValue(storeType, appName);
                        }
                    }
                }
            }
        }

    OK,主域名和二级域名都实现上面的三个步骤就可以了。本文为拎壶充原创文章,转载请注明原文地址:http://www.cnblogs.com/liaohuolin/p/4502554.html

  • 相关阅读:
    Sage CRM升级后的问题跟进
    js 控制滚动条位置
    初学Jquery插件制作:在SageCRM的查询屏幕隐藏部分行的功能
    [转摘] Reporting Service 200
    js数组去重复项
    JavaScript tips 1024 * 768
    javascript 的 replace 函数
    the secret of sagecrm urls
    Sage CRM 自增ID的方案和遇到的问题
    EXTJS 可编辑列表的时间编辑问题
  • 原文地址:https://www.cnblogs.com/liaohuolin/p/4502554.html
Copyright © 2020-2023  润新知