【问题标题】:sharing session data between domains in asp.net, confused!在asp.net中的域之间共享会话数据,困惑!
【发布时间】:2011-02-27 09:31:47
【问题描述】:

好的,这是我的问题,我想维护两个应用程序或域之间的会话数据(例如:- www.abc.com 和 secure.abc.com)。

我在网上读到过这方面的内容,但很多人指出了许多不同的方法,人们评论了 +ve 和 -ve 对所有人的回应。另外很多只是提供理论答案,做这个做那个,但根本没有代码。

是否只需要这些步骤? 1)在web.config中:<httpCookies domain=".abc.com"/>

2)将会话数据存储在sql DB中:(在准备好用于存储会话的数据库之后)

<sessionState mode="SQLServer" sqlConnectionString="Data Source=YourServer;
Integrated Security=True;database=MySessionDB" sqlCommandTimeout="30" 
allowCustomSqlDatabase="true"/>
<machineKey decryption="AES" validation="SHA1" decryptionKey="..." validationKey="..." />

3)我对此感到困惑:我想像这样为会话 cookie 设置域 Response.Cookies["ASP.NET_SessionId"].Domain = ".abc.com"; 但是这段代码应该写在哪里呢? 此条目:http://mgrzyb.blogspot.com/2007/12/aspnet-and-subdomains.html 说:使用 System.Web.SessionState.SessionIDManager 作为基类,但 SaveSessionID 方法不是虚拟的,因此不能被覆盖。选项是:显式重新实现接口方法或装饰 SessionIDManager 类,并在调用 SessionIDManager.SaveSessionID 后将 Response.Cookies[SessionIdCookieName].Domain 设置为我们的域。

只有作者提供了真实代码,第 3 步才会清晰。

谁能提供它的代码。

加上这 3 个步骤足以在域之间共享会话?

【问题讨论】:

    标签: asp.net subdomain


    【解决方案1】:

    第三步语句可以写在global.asax中,根据:http://www.know24.net/blog/ASPNET+Session+State+Cookies+And+Subdomains.aspx

    protected  void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    
    {
    
      /// only apply session cookie persistence to requests requiring session information
    
    
    
      #region session cookie
    
      if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState )
    
      {
    
        /// Ensure ASP.NET Session Cookies are accessible throughout the subdomains.
    
    
    
        if (Request.Cookies["ASP.NET_SessionId"] != null && Session != null && Session.SessionID != null)
    
        {
    
          Response.Cookies["ASP.NET_SessionId"].Value = Session.SessionID;
    
          Response.Cookies["ASP.NET_SessionId"].Domain = ".abc.com"; // the full stop prefix denotes all sub domains
    
          Response.Cookies["ASP.NET_SessionId"].Path = "/"; //default session cookie path root         
    
        }
    
      }
    
      #endregion    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-16
      • 2017-06-11
      • 2013-03-12
      • 2016-12-27
      • 2013-02-15
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多