2

私も持っていwww.example.comますstore.example.com。(はい、それらは同じ親ドメインのサブドメインです)

store.example.comASP.NET 1.1上にあります

www.example.comASP.NET 3.5上にあります

2 つのサイト間で「セッション」データを共有するために利用できるオプションを知りたいです。なんらかの共有ログインと、ユーザーがどのサイトから開始したかに関係なく、ユーザー アクティビティを追跡する機能が必要です。

  • 明らかに、あるサイトから別のサイトに移行するときに GUID を送信できます。

  • また、サブドメイン間で共有できる Cookie を設定できると思います。私はこれを試したことはありませんが、おそらく私がやろうとしていることです。これが真の「セッション」Cookie なのか、それとも有効期限を短く設定しただけなのかはまだわかりません。

これらは私の最良の選択肢ですか、それとも他に何かありますか?

4

3 に答える 3

6

異なるアプリ間でセッションを共有したい場合、いくつかのことを行う必要があります。

まず、セッション状態を SQL モードで実行する必要があります。この時点で、SQL セッション状態がマシン キーと _appDomainAppId を使用して、アプリが独自のセッション データにアクセスするためのキーを生成することがわかりました。そのため、すべてのアプリでこれらを同じに保つ必要があります。

アプリの Web 構成では、同じマシン キーを使用する必要があります。これは、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";

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

そして、それはうまくいくはずです。

于 2009-08-13T11:41:06.003 に答える
3

重要なことは、Cookie ドメインを適切に設定することです。

ドメインが.example.comに設定されている場合(先頭のピリオドに注意してください)、example.com およびすべてのサブドメインへのリクエストに含める必要があります。

異なるサブドメイン間でデータを共有する方法があると思います。

于 2008-10-29T07:10:45.367 に答える
-3

PHPでそれを行う方法は次のとおりです。

http://php.dtbaker.com.au/post/keeper_cookies_across_multiple_sub_domains.html

于 2008-10-29T06:34:14.427 に答える