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

问题描述:

我有www.example.comstore.example.com。 (是的,他们是同父域的子域)有哪些选项可用于跨子域共享数据?

store.example.com是ASP.NET 1.1

www.example.com是在ASP.NET 3.5

我想知道哪些选项可用于共享“会议'两个网站之间的数据。我需要某种共享登录,并且还需要能够跟踪用户活动的情况,而不管他们开始使用哪个网站。

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

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

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

+0

我相信,你现在可能已经得到了答案,但只想发布链接到这里的答案在这里http://*.com/questions/2868316/sharing-sessions-across-applications-using -the-asp-net-session-state-service/3151315#3151315 – Vamsi 2011-11-16 05:37:08

+0

这是一个asp.net问题。 。 。 – andrewWinn 2009-08-13 11:51:07

做最重要的事情是正确设置Cookie域。

它的域名设置为.example.com(注意前导期),那么它应该包含在对example.com和所有子域的请求中。

我假设你有一种方式来共享你的不同子域之间的数据。

+0

我爱你如何在最后把你的问题作为一个假设来表示;-) 感谢您的信息 – 2008-10-29 07:33:30

+0

对不起,我认为你的问题是关于识别属于同一个人的2个不同子域的HTTP请求。 我假设你知道如何连接到例如来自两个不同地方(每个子域的代码)的数据库 - 是否有错?我想我对此并不完全清楚 – Gareth 2008-10-29 12:20:58

如果你想分享不同应用程序之间的会话,你需要做几件事情。

首先,您需要在SQL模式下运行会话状态。 在这一点上,我发现SQL会话状态需要机器密钥和_appDomainAppId为您的应用程序生成一个密钥,以访问它自己的会话数据。所以我们需要在所有应用程序中保持相同。

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

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

添加appSetting“应用程序名称”,并给它的名称(这已是这两个应用是相同的) 然后,您需要创建一个共享会话模块将更改_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 
    } 

这应该做的伎俩。