有什么选择有跨子域共享数据? [英] What options are there for sharing data across subdomains?

查看:146
本文介绍了有什么选择有跨子域共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

store.example.com 是ASP.NET 1.1

www.example.com 是ASP.NET 3.5

我想知道哪些选项可用于在两个站点之间共享会话数据。我需要某种形式的共享登录的,也是abiltity跟踪用户活动,无论他们开始在其网站上。


  • Obvously从一个网站到另一个过渡时,我可以把一个GUID。


  • 我也相信我可以设置哪些可以跨子域共享一个cookie。我从来没有试过,但它是最有可能我会做什么。我还不太清楚,如果这是一个真正的会话饼干或者,如果我只是设置一个低的截止日期?


这些是我最好的选择还是有别的事端?


解决方案

如果你想分享不同的应用程序之间的会话有你需要做一些事情。

首先,您需要运行在SQL模式会话状态。
在这一点上,我发现,在SQL会话状态需要的计算机密钥和你_appDomainAppId生成您的应用程序来访问它自己的会话数据的关键。因此,我们需要把这些所有的应用程序之间是相同的。

在您的应用程序的Web CONFIGS你需要使用同一台机器密钥。这可以是任何地方的System.Web标签里面例如:

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

添加appSetting应用程序名称,并给它的名称(这已成为这两个应用是相同的)
然后,您需要创建一个共享的会话模块,这将改变_appDomainAppId。下一个是我使用。

 命名YourApp
{
  使用System.Configuration;
  使用的System.Reflection;
  使用的System.Web;  ///<总结>用于共享应用程序域℃之间的会话类; /总结>
  公共类SharedSessionModule:IHttpModule的
  {
    #区域IHttpModule的成员
    ///<总结>
    ///初始化模块和prepares它来处理请求。
    ///< /总结>
    ///< PARAM NAME =背景>一种<见CREF =T:System.Web.HttpApplication/>
    ///提供访问方法,
    ///属性和共同活动,所有应用程序对象在ASP.NET中
    ///应用与LT; /参数>
    ///<创建日期= =由2008年5月31日彼得Femiani/>
    公共无效初始化(HttpApplication的情况下)
    {
      //从配置文件中的应用程序名称...
      字符串的appName = ConfigurationManager.AppSettings [应用程序名称];
      如果(!string.IsNullOrEmpty(的appName))
      {
        字段信息runtimeInfo = typeof运算(的httpRuntime).GetField(_ theRuntime,BindingFlags.Static | BindingFlags.NonPublic可);
        的httpRuntime theRuntime =(的httpRuntime)runtimeInfo.GetValue(NULL);
        字段信息appNameInfo = typeof运算(的httpRuntime).GetField(_ appDomainAppId,BindingFlags.Instance | BindingFlags.NonPublic可);
        appNameInfo.SetValue(theRuntime,的appName);
      }
    }    ///<总结>
    ///由模块使用的资源(内存除外)处置的
    ///工具<见CREF =T:System.Web.IHttpModule/取代。
    ///< /总结>
    ///<创建日期= =由2008年5月31日彼得Femiani/>
    公共无效的Dispose()
    {
    }
    #endregion
  }
}

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

 <添加名称=SharedSessionModuleTYPE =YourApp.SharedSessionModule,YourApp,版本= 1.0.0.0,文化=中性/>

最后要做的是让会话cookie来域之间传递......像这样


  VAR会话= HttpContext.Current.Session;
  VAR请求= HttpContext.Current.Request;
  VAR饼干= Request.Cookies时[ASP.NET_SessionId];
  如果(饼干= NULL&放大器;!&安培;!会话= NULL&放大器;&安培;!session.SessionID = NULL)
  {
    cookie.Value = session.SessionID;
    cookie.Domain =yourappdomain.com;    //句号preFIX表示所有子域名
    cookie.Path =/; //默认的会话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?

解决方案

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.

这篇关于有什么选择有跨子域共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆