異なる2級ドメイン名の下でSessionが共有する問題の解決


まずwebconfigの構成を見てみましょう
2つの状況に分けて、具体的な配置は以下の
web.configの構成は以下の通りである:IIS 6:
  <system.web>

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

    <httpModules>
      <add name="SessionSharedHttpModule" type="Myun.Web.SessionSharedHttpModule, Myun.Web" />
    </httpModules>

  </system.web>

 
IIS 7:管理パイプモードは集積パイプモードであり、そうでなければIIS 6と同じである
 
 <system.web>


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


  </system.web>


 <system.webServer>


    <modules>
      <add name="SessionSharedHttpModule" type="Myun.Web.SessionSharedHttpModule, Myun.Web" />
    </modules>


 </system.webServer>

次のバックグラウンドコードの処理:
コードは次のとおりです.cs

//****************************//

//create date:2010-01-11

//****************************//

namespace YeWenBin.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.SessionState;
    using System.Reflection;
    /// <summary>
    ///            IHttpModule
    /// </summary>
    public class SessionSharedHttpModule : IHttpModule
    {
        string _rootDomain = null; //    


        public void Dispose()
        {
            //throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            _rootDomain = "yewenbin.cn"; //      

            //          ( www.yewenbin.cn  yewenbin.cn,              )
            //_rootDomain = _rootDomain.Substring(_rootDomain.LastIndexOf('.', _rootDomain.LastIndexOf('.') - 1) + 1);
            
            //           OutOfProcSessionStateStore            s_uribase
            //OutOfProcSessionStateStore    :
            //internal sealed class OutOfProcSessionStateStore : SessionStateStoreProviderBase
            //s_uribase    :
            //static string       s_uribase;
            //  OutOfProcSessionStateStore   s_uribase        OutOfProcStateClientManager.cs  
            //    :Framework   \V2.0.5727\dd
dp\fx\src\xsp\System\Web\State\OutOfProcStateClientManager.cs 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, _rootDomain); context.EndRequest += new EventHandler(context_EndRequest); } /// <summary> /// Cookie ID Cookie /// Domain /// </summary> void context_EndRequest(object sender, System.EventArgs e) { HttpApplication app = sender as HttpApplication; for (int i = app.Context.Response.Cookies.Count - 1; i >= 0; i--) { //ASP.NET_SessionId ID key, if (app.Context.Response.Cookies[i].Name.Equals("ASP.NET_SessionId")) { app.Context.Response.Cookies[i].Domain = _rootDomain; return; } } } } }