有哪些选项可用于跨子域共享数据?

发布于 2024-07-08 17:48:37 字数 480 浏览 6 评论 0原文

我有 www.example.comstore.example.com。 (是的,它们是同一父域的子域)

store.example.com 位于 ASP.NET 1.1

www.example.com 位于 ASP.NET 3.5

我想要了解哪些选项可用于在两个站点之间共享“会话”数据。 我需要某种共享登录以及跟踪用户活动的能力,无论他们从哪个网站开始。

  • 显然,当从一个站点转换到另一个站点时,我可以发送 GUID。 显然

  • 我还相信我可以设置一个可以跨子域共享的 cookie。 我从未尝试过这个,但这很可能是我会做的。 我还不清楚这是否是真正的“会话”cookie,或者我只是设置了一个较低的到期日期?

这些是我最好的选择还是还有其他选择?

I have www.example.com and also store.example.com.
(Yes they are subdomains of the same parent domain)

store.example.com is on ASP.NET 1.1

www.example.com is on ASP.NET 3.5

I want to know what options are available for sharing 'session' data between the two sites. I need some kind of shared login and also the abiltity to track user activity no matter which site they started on.

  • Obvously I could send a GUID when transitioning from one site to the other.

  • I also believe I can set a cookie which can be shared across subdomains. I've never tried this but it is most likely what I will do. I'm not yet clear if this is a true 'session' cookie or if I just set a low expiration date?

Are these my best options or is there somethin else?

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

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

发布评论

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

评论(3

白况 2024-07-15 17:48:37

如果您想在不同应用程序之间共享会话,则需要执行一些操作。

首先,您需要在 SQL 模式下运行会话状态。
此时,我发现 SQL 会话状态使用计算机密钥和您的 _appDomainAppId 来为您的应用程序生成一个密钥来访问它自己的会话数据。 因此,我们需要在所有应用程序之间保持这些相同。

在应用程序的网络配置中,您需要使用相同的机器密钥。 这可以是 system.web 标签内的任何位置 EG:

    <machineKey decryptionKey="EDCDA6DF458176504BBCC720A4E29348E252E652591179E2" validationKey="CC482ED6B5D3569819B3C8F07AC3FA855B2FED7F0130F55D8405597C796457A2F5162D35C69B61F257DB5EFE6BC4F6CEBDD23A4118C4519F55185CB5EB3DFE61"/>

添加 appSetting“ApplicationName”并为其命名(两个应用程序必须相同)
然后,您需要创建一个共享会话模块,该模块将更改 _appDomainAppId。 下面这个是我用的。

    namespace YourApp
{
  using System.Configuration;
  using System.Reflection;
  using System.Web;

  /// <summary>class used for sharing the session between app domains</summary>
  public class SharedSessionModule : IHttpModule
  {
    #region IHttpModule Members
    /// <summary>
    /// Initializes a module and prepares it to handle requests.
    /// </summary>
    /// <param name="context">An <see cref="T:System.Web.HttpApplication"/>
    /// that provides access to the methods,
    /// properties, and events common to all application objects within an ASP.NET
    /// application</param>
    /// <created date="5/31/2008" by="Peter Femiani"/>
    public void Init(HttpApplication context)
    {
      // Get the app name from config file...
      string appName = ConfigurationManager.AppSettings["ApplicationName"];
      if (!string.IsNullOrEmpty(appName))
      {
        FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
        HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
        FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
        appNameInfo.SetValue(theRuntime, appName);
      }
    }

    /// <summary>
    /// Disposes of the resources (other than memory) used by the module that
    /// implements <see cref="T:System.Web.IHttpModule"/>.
    /// </summary>
    /// <created date="5/31/2008" by="Peter Femiani"/>
    public void Dispose()
    {
    }
    #endregion
  }
}

在 Web 配置中,您需要添加此模块:

      <add name="SharedSessionModule" type="YourApp.SharedSessionModule, YourApp, Version=1.0.0.0, Culture=neutral" />

最后要做的事情是允许会话 cookie 在域之间传递...就像这样

 var session = HttpContext.Current.Session; 
    var request = HttpContext.Current.Request; 
    var cookie = request.Cookies["ASP.NET_SessionId"]; 
    if (cookie != null && session != null && session.SessionID != null) 
    { 
      cookie.Value = session.SessionID; 
      cookie.Domain = "yourappdomain.com"; 

      // 句号前缀表示所有子域 
      cookie.Path = "/";   // 默认会话cookie路径根 
    } 
  

这应该可以解决问题。

If you want to share sessions between different apps there are a few things you need to do.

First you'll need to run the session state in SQL mode.
At this point I found out that the SQL session state takes the machine key and your _appDomainAppId to generate a key for your app to access it's own session data. So we need to keep these the same between all your apps.

In the web configs of your apps you'll need to use the same machine key. This can be any where inside the system.web tags E.G:

    <machineKey decryptionKey="EDCDA6DF458176504BBCC720A4E29348E252E652591179E2" validationKey="CC482ED6B5D3569819B3C8F07AC3FA855B2FED7F0130F55D8405597C796457A2F5162D35C69B61F257DB5EFE6BC4F6CEBDD23A4118C4519F55185CB5EB3DFE61"/>

Add an appSetting "ApplicationName" and give it name (this has to be the same for both apps)
You'll then need to create a shared session module which will change the _appDomainAppId. The one below is what I use.

    namespace YourApp
{
  using System.Configuration;
  using System.Reflection;
  using System.Web;

  /// <summary>class used for sharing the session between app domains</summary>
  public class SharedSessionModule : IHttpModule
  {
    #region IHttpModule Members
    /// <summary>
    /// Initializes a module and prepares it to handle requests.
    /// </summary>
    /// <param name="context">An <see cref="T:System.Web.HttpApplication"/>
    /// that provides access to the methods,
    /// properties, and events common to all application objects within an ASP.NET
    /// application</param>
    /// <created date="5/31/2008" by="Peter Femiani"/>
    public void Init(HttpApplication context)
    {
      // Get the app name from config file...
      string appName = ConfigurationManager.AppSettings["ApplicationName"];
      if (!string.IsNullOrEmpty(appName))
      {
        FieldInfo runtimeInfo = typeof(HttpRuntime).GetField("_theRuntime", BindingFlags.Static | BindingFlags.NonPublic);
        HttpRuntime theRuntime = (HttpRuntime)runtimeInfo.GetValue(null);
        FieldInfo appNameInfo = typeof(HttpRuntime).GetField("_appDomainAppId", BindingFlags.Instance | BindingFlags.NonPublic);
        appNameInfo.SetValue(theRuntime, appName);
      }
    }

    /// <summary>
    /// Disposes of the resources (other than memory) used by the module that
    /// implements <see cref="T:System.Web.IHttpModule"/>.
    /// </summary>
    /// <created date="5/31/2008" by="Peter Femiani"/>
    public void Dispose()
    {
    }
    #endregion
  }
}

In the web config you'll need to add this module:

      <add name="SharedSessionModule" type="YourApp.SharedSessionModule, YourApp, Version=1.0.0.0, Culture=neutral" />

Final thing to do is to allow the session cookie to pass between domains...like so

  var session = HttpContext.Current.Session;
  var request = HttpContext.Current.Request;
  var cookie = request.Cookies["ASP.NET_SessionId"];
  if (cookie != null && session != null && session.SessionID != null)
  {
    cookie.Value = session.SessionID;
    cookie.Domain = "yourappdomain.com";

    // the full stop prefix denotes all sub domains
    cookie.Path = "/"; // default session cookie path root
  }

And that should do the trick.

木格 2024-07-15 17:48:37

重要的是正确设置 cookie 域。

如果域设置为 .example.com(请注意前导句点),则应将其包含在对 example.com 以及所有子域的请求中。

我假设您有一种在不同子域之间共享数据的方法。

The important thing to do is to set the cookie domain properly.

It the domain is set to .example.com (note the leading period) then it should be included in requests to example.com and also all of the subdomains.

I assume you have a way of sharing the data between your different subdomains.

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