.net主站和次層網域下實現session共用

來源:互聯網
上載者:User

標籤: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         #endregion38     }

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 Internet16         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共用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.