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

.net主站和二级域名下实现session共享

时间:2017-02-23 13:47:50      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:handle   div   tor   context   member   previous   hand   bin   域名   

 1     public class CrossDomainCookie : IHttpModule
 2     {
 3         private string m_RootDomain = string.Empty;
 4 
 5         #region IHttpModule Members
 6 
 7         public void Dispose()
 8         {
 9             
10         }
11 
12         public void Init(HttpApplication context)
13         {
14             m_RootDomain = ConfigurationManager.AppSettings["RootDomain"];
15 
16             Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
17             FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);
18 
19             if (uriField == null)
20                 throw new ArgumentException("UriField was not found");
21 
22             uriField.SetValue(null, m_RootDomain);
23             
24             context.EndRequest += new System.EventHandler(context_EndRequest);
25         }
26 
27         void context_EndRequest(object sender, System.EventArgs e)
28         {
29             HttpApplication app = sender as HttpApplication;
30 
31             for (int i = 0; i < app.Context.Response.Cookies.Count; i++)
32             {
33                 app.Context.Response.Cookies[i].Domain = m_RootDomain;
34             }
35         }
36 
37         #endregion
38     }

config里面配置:(注意两个站点都要配置)

 1 <configuration>
 2   <appSettings>
 3     <add key="RootDomain" value=".session.com"/>
 4   </appSettings>
 5   <connectionStrings/>
 6   <system.web> 
 7     <machineKey decryptionKey="FD69B2EB9A11E3063518F1932E314E4AA1577BF0B824F369" validationKey="5F32295C31223A362286DD5777916FCD0FD2A8EF882783FD3E29AB1FCDFE931F8FA45A8E468B7A40269E50A748778CBB8DB2262D44A86BBCEA96DCA46CBC05C3" validation="SHA1" decryption="Auto"/>
 8     <sessionState cookieless="false" timeout="50" mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424"/>
 9     <httpModules>
10       <add name="CrossDomainCookieModule" type="WebLibrary.CrossDomainCookie, WebLibrary"/>
11     </httpModules>
12     <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
13   </system.web>
14   <!-- 
15         The system.webServer section is required for running ASP.NET AJAX under Internet
16         Information Services 7.0.  It is not necessary for previous version of IIS.
17     -->
18   <system.webServer>
19     <modules>
20       <add name="CrossDomainCookieModule" preCondition="managedHandler" type="WebLibrary.CrossDomainCookie, WebLibrary"/>
21     </modules>
22   </system.webServer>
23 </configuration>

 

.net主站和二级域名下实现session共享

标签:handle   div   tor   context   member   previous   hand   bin   域名   

原文地址:http://www.cnblogs.com/ksy20151123/p/6432961.html

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