码迷,mamicode.com
首页 > Web开发 > 详细

.net多站点通过StateServer实现session共享

时间:2015-01-08 12:55:42      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

 

先在所有要共享站点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_MACHINE\SYSTEM\CurrentControlSet\Services \aspnet_state\Parameters中的AllowRemoteConnection 键的值为1,其中的Port键控制ASP.NET State Service的监听端口;修改后需要重启ASP.NET State Service才生效;

.net多站点通过StateServer实现session共享

标签:

原文地址:http://www.cnblogs.com/q149072205/p/4210475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!