在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原文地址: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"/>
<httpModules>
<add name="CrossDomainCookieModule" type="WebLibrary.CrossDomainCookie, WebLibrary"/> </httpModules>
<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" timeout="30" />
<modules>
<add name="CrossDomainCookieModule" preCondition="managedHandler" type="WebLibrary.CrossDomainCookie, WebLibrary"/> </modules>
|
请发表评论