zoukankan      html  css  js  c++  java
  • ASP.NET中解决跨子域的Session共享

    原文地址:http://www.itivy.com/ivy/archive/2011/4/24/634392360221984930.html

    新建一个类库项目,添加以下类:

    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 = string.Empty;

             #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
         }
     } 

    把它编译成dll。

    然后在每个需要共享session的网站项目中做以下几件事情:

    1、引用上面的dll

    2、在web.config的appsetting节点中添加你的主站域名

    <add key="RootDomain" value=".itivy.com"/> 


    3、在web.config的system.web节点中添加

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


     4、在web.config的system.web节点中修改session存储方式为stateserver

    <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" timeout="30" /> 


    5、在web.config的system.webServer几点中添加

    <modules>
        <add name="CrossDomainCookieModule" preCondition="managedHandler" type="WebLibrary.CrossDomainCookie, WebLibrary"/> 
    </modules> 


     

  • 相关阅读:
    Apache OFBiz 学习笔记 之 实体引擎
    Apache OFBiz 学习笔记 之 服务引擎 二
    Apache OFBiz 学习笔记 之 服务引擎 一
    [OFBiz]开发 五
    [OFBiz]开发 四
    [OFBiz]开发 三
    [OFBiz]开发 二
    通过Wmi实现Hyper-V远程管理(一)
    wifi智能插座 一键扫描局域网内插座Ip及其它信息 Python源代码API
    .NET4.0下使用Net2.0类库或程序集
  • 原文地址:https://www.cnblogs.com/flycantus/p/2599778.html
Copyright © 2011-2022 走看看