从微软一站式代码库中学到的--跨域之间的session共享
據說下面的這段代碼這件可以實現不同域之間的session共享:
namespace CSASPNETShareSessionBetweenSubDomainsModule
{
??? /// <summary>
??? /// A HttpModule used for sharing the session between Applications in
??? /// sub domains.
??? /// </summary>
??? public class SharedSessionModule : IHttpModule
??? {
??????? // Cache settings on memory.
??????? protected static string applicationName = ConfigurationManager.AppSettings["ApplicationName"];
??????? protected static string rootDomain = ConfigurationManager.AppSettings["RootDomain"];
??????? #region IHttpModule Members
??????? /// <summary>
??????? /// Initializes a module and prepares it to handle requests.
??????? /// </summary>
??????? /// <param name="context">
??????? /// An System.Web.HttpApplication
??????? /// that provides access to the methods,
??????? /// properties, and events common to all application objects within
??????? /// an ASP.NET application.
??????? /// </param>
??????? public void Init(HttpApplication context)
??????? {
??????????? // This module requires both Application Name and Root Domain to work.
??????????? if (string.IsNullOrEmpty(applicationName) ||
??????????????? string.IsNullOrEmpty(rootDomain))
??????????? {
??????????????? return;
??????????? }
??????????? // Change the Application Name in runtime.
??????????? 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, applicationName);
??????????? // Subscribe Events.
??????????? context.PostRequestHandlerExecute += new EventHandler(context_PostRequestHandlerExecute);
??????? }
??????? /// <summary>
??????? /// Disposes of the resources (other than memory) used by the module
??????? /// that implements.
??????? /// </summary>
??????? public void Dispose()
??????? {
??????? }
??????? #endregion
??????? /// <summary>
??????? /// Before sending response content to client, change the Cookie to Root Domain
??????? /// and store current Session Id.
??????? /// </summary>
??????? /// <param name="sender">
??????? /// An instance of System.Web.HttpApplication that provides access to
??????? /// the methods, properties, and events common to all application
??????? /// objects within an ASP.NET application.
??????? /// </param>
??????? void context_PostRequestHandlerExecute(object sender, EventArgs e)
??????? {
??????????? HttpApplication context = (HttpApplication)sender;
??????????? // ASP.NET store a Session Id in cookie to specify current Session.
??????????? HttpCookie cookie = context.Response.Cookies["ASP.NET_SessionId"];
??????????? if (context.Session != null &&
??????????????? !string.IsNullOrEmpty(context.Session.SessionID))
??????????? {
??????????????? // Need to store current Session Id during every request.
??????????????? cookie.Value = context.Session.SessionID;
??????????????? // All Applications use one root domain to store this Cookie
??????????????? // So that it can be shared.
??????????????? if (rootDomain != "localhost")
??????????????? {
??????????????????? cookie.Domain = rootDomain;
??????????????? }
??????????????? // All Virtual Applications and Folders share this Cookie too.
??????????????? cookie.Path = "/";
??????????? }
??????? }
??? }
}
?
兩個項目分別引用一個這個類,就用實現跨域之間的session共享。
轉載于:https://www.cnblogs.com/zhaojialong/archive/2011/09/28/2194541.html
總結
以上是生活随笔為你收集整理的从微软一站式代码库中学到的--跨域之间的session共享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式第三集——装饰者模式(Decor
- 下一篇: 如何搭建一个 Data Guard 环境