在子域之间共享数据有哪些选择?

Sim*_*ver 2 asp.net subdomain session

www.example.com也有store.example.com.(是的,它们是同一父域的子域)

store.example.com 在ASP.NET 1.1上

www.example.com 在ASP.NET 3.5上

我想知道哪些选项可用于在两个站点之间共享"会话"数据.我需要某种共享登录以及跟踪用户活动的能力,无论他们开始使用哪个站点.

  • 在从一个站点转换到另一个站点时,我可以发送GUID.

  • 我也相信我可以设置一个可以跨子域共享的cookie.我从来没有尝试过,但很可能是我会做的.我还不清楚这是一个真正的'会话'cookie还是我只设置了一个低的到期日期?

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

TWi*_*ars 6

如果您想在不同的应用程序之间共享会话,您需要做一些事情.

首先,您需要在SQL模式下运行会话状态.此时我发现SQL会话状态采用机器密钥,而_appDomainAppId生成一个密钥,供您的应用访问自己的会话数据.因此,我们需要在所有应用之间保持相同.

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

    <machineKey decryptionKey="EDCDA6DF458176504BBCC720A4E29348E252E652591179E2" validationKey="CC482ED6B5D3569819B3C8F07AC3FA855B2FED7F0130F55D8405597C796457A2F5162D35C69B61F257DB5EFE6BC4F6CEBDD23A4118C4519F55185CB5EB3DFE61"/>
Run Code Online (Sandbox Code Playgroud)

添加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
  }
}
Run Code Online (Sandbox Code Playgroud)

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

      <add name="SharedSessionModule" type="YourApp.SharedSessionModule, YourApp, Version=1.0.0.0, Culture=neutral" />
Run Code Online (Sandbox Code Playgroud)

最后要做的是允许会话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";

    // the full stop prefix denotes all sub domains
    cookie.Path = "/"; // default session cookie path root
  }
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题.