在asp.net中的域之间共享会话数据,困惑!

发布于 2024-09-04 20:50:42 字数 1204 浏览 4 评论 0原文

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

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

这些步骤就是所需要的全部吗? 1) 在 web.config 中:

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)Am对这个感到困惑:我想像这样设置会话 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 个步骤足以在域之间共享会话吗?

Ok, here's my problem, i want to maintain session data between two applications or domains (eg:- www.abc.com and secure.abc.com).

I have read on net about this, but many people pointing many different ways to do it, with people commenting +ve and -ve responses to all. Plus many are just providing theoretical answer, do this and that ,but no code at all.

are these steps all that is required?
1) in web.config: <httpCookies domain=".abc.com"/>

2) store session data in sql DB as:(after preparing the db for storing sessions)

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

3)Am confused about this one: i want to set the domain for the session cookie like this
Response.Cookies["ASP.NET_SessionId"].Domain = ".abc.com";
But where should this code be written?
this entry: http://mgrzyb.blogspot.com/2007/12/aspnet-and-subdomains.html says: use System.Web.SessionState.SessionIDManager as a base class but the SaveSessionID method is not virtual so cannot be overridden. Options are: either explicitly re-implement the interface method or decorate SessionIDManager class and after calling SessionIDManager.SaveSessionID set Response.Cookies[SessionIdCookieName].Domain to our domain.

Only if the author had provided real code, step 3 would have been clear.

Can anyone provide the code for it.

Plus all this 3 steps enough to share session among the domains?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-09-11 20:50:42

第三步语句可以根据以下内容编写在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    

}

the 3rd step statement can be written in global.asax according to: 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    

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文