一、实现IHttpModule类,见后。
二、修改web.config
添加域名
<appSettings><add key="RootDomain" value=""/></appSettings>
RootDomain cookie域名,若是本地则写空
by design domain names must have at least two dots otherwise browser will say they are invalid
when working on localhost (!) the cookie-domain must be set to "" or NULL or FALSE instead of "localhost"
增加过滤
<system.web>
<sessionState mode="StateServer" stateNetworkTimeout="10" timeout="120" stateConnectionString="tcpip=127.0.0.1:42424" />
<httpModules>
<add name="SessionSharedHttpModule" type="ClassLibrary.BasePage.MakeSessionIDOneOnly,ClassLibrary"/>
</httpModules>
</system.web>
三、附类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Reflection;
using System.Configuration;
namespace ClassLibrary.BasePage
{
public class MakeSessionIDOneOnly : 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++)
{
if (app.Context.Response.Cookies[i].Name == "ASP.NET_SessionId")
{
app.Context.Response.Cookies[i].Domain = m_RootDomain;
}
}
}
#endregion
}
}
参考:Session共享的解决方案