zoukankan      html  css  js  c++  java
  • .net多站点通过StateServer实现session共享

    先在所有要共享站点web.config的<system.web>结点下加

    <!--session的mode=StateServer-->
    <
    sessionState cookieless="false" timeout="50" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"/>
    <!--机器码要统一--> <machineKey decryptionKey="FD69B2EB9A11E3063518F1932E314E4AA1577BF0B824F369" validationKey="5F32295C31223A362286DD5777916FCD0FD2A8EF882783FD3E29AB1FCDFE931F8FA45A8E468B7A40269E50A748778CBB8DB2262D44A86BBCEA96DCA46CBC05C3" validation="SHA1" decryption="Auto"/> <httpModules>

      <add name="CrossDomainCookieModule" type="WebLibrary.CrossDomainCookie, WebLibrary"/> 
        </httpModules>

    新建一个类库项目代码如下

    using System.Web; 
    using System; 
    using System.Configuration; 
    using System.Diagnostics; 
    using System.Reflection; 
    using System.Web.SessionState;   
    
    namespace WebLibrary 
    {
         public class CrossDomainCookie : IHttpModule
         {
             private string m_RootDomain = “http://localhost:8008/”;//主要共享的Session的URL,这里测试仪所以用locahlost,也可以用域名
    
             #region IHttpModule Members
             public void Dispose() 
             {  
             } 
             public void Init(HttpApplication context)
             {
                 m_RootDomain = ConfigurationManager.AppSettings["RootDomain"];
                 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, m_RootDomain); 
    
                 context.EndRequest += new System.EventHandler(context_EndRequest);
             }
             void context_EndRequest(object sender, System.EventArgs e)
             {
                 HttpApplication app = sender as HttpApplication;
                 for (int i = 0; i < app.Context.Response.Cookies.Count; i++)
                 {
                     app.Context.Response.Cookies[i].Domain = m_RootDomain; 
                 }
             }
             #endregion
         }
     }

    若StateServer为远程服务器不是127.0.0.1,则为IpAddress为远程服务器IP地址修改Session服务器注册表中的项 HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices aspnet_stateParameters中的AllowRemoteConnection 键的值为1,其中的Port键控制ASP.NET State Service的监听端口;修改后需要重启ASP.NET State Service才生效;

  • 相关阅读:
    redis实现与分析
    NULL, '',0 '0'的区别
    Linux strace命令
    strcpy和memcpy的区别
    图书推荐
    php与mysql通讯那点事
    linux命令汇总
    linux系统信息查询及相关概念
    LNMP zabbix安装
    lftp查看文件时间与登录服务查看文件时间相差8小时
  • 原文地址:https://www.cnblogs.com/q149072205/p/4210475.html
Copyright © 2011-2022 走看看