据说下面的这段代码这件可以实现不同域之间的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共享相关推荐

  1. 微软一站式示例脚本库 — IT专业人士的省时利器

    众所周知,IT专业人士所从事的是一项高压力,忙碌又具有挑战性的工作.在无数个不眠之夜,他们中的很多人都需要高度专注地完成一些重复性的工作,同时又在心中不停提醒自己不要因为任务简单而犯错.作为IT专业人 ...

  2. All-In-One Code Framework [一站式示例代码库] 【转】

    All-In-One Code Framework [一站式示例代码库] 2010 对一站式示例代码库,对奋战在一站式示例代码库上的每一位工程师来说都是不同寻常的一年. 在我们共同努力和开发社区的支持 ...

  3. 微软一站式示例代码库 7月新代码示例发布

    微软一站式示例代码库 7月新代码示例发布. 下载地址: http://1code.codeplex.com/releases/view/69942 你也可以通过 示例代码浏览器 或 示例代码浏览器Vi ...

  4. 微软一站式示例代码库 2012 年2月示例代码更新。8个全新示例为您的开发保驾护航...

    微软一站式示例代码库2012年2月新代码示例发布.我们发布了8个全新的示例代码,内容包含Windows Azure, Directory Services, Hyper-V, TFS, WDK和Win ...

  5. 微软一站式示例代码库 8 月新代码示例发布

    微软一站式示例代码库 8 月新代码示例发布. 下载地址: http://1code.codeplex.com/releases/view/71395 你也可以通过 示例代码浏览器 或 示例代码浏览器V ...

  6. 微软一站式示例代码库(中文版)2011-07-14版本, 新添加ASP.NET, Azure, Silverlight, WinForm等14个Sample...

    暑假到了,绿树阴浓夏日长,楼台倒影入池塘,水精帘动微风起,满架蔷薇一院香. 我们发布了新一版本的All-In-One Code Framework 中文版,新增14个Sample.包括5个ASP.NE ...

  7. 微软一站式示例代码库(中文版)2012-2-10版本, 新添加ASP.NET, Windows Form, VSX, Windows Shell, WPF等16个Sample

    让大家久等了,2月份我们准备了16个中文版Microsoft OneCode Sample,其中包括2个Windows Form Sample,2个VSX Sample,1个Windows Shell ...

  8. 直击微软第九频道著名主持Robert Green 对话一站式示例代码库大老板梁梅女士

    今晨,微软第九频道(Channel 9)著名主持人Robert Green 对话一站式示例代码库Group Manager 梁梅女士.梁梅女士介绍并演示了由一站式示例代码库携手Visual Studi ...

  9. VC++, VC#, VB.NET 编码规范 (微软一站式示例代码库)

    vb.net教程https://www.xin3721.com/eschool/vbnetxin3721/ 经过三个月的不懈努力和反复检查,我们项目组终于完成了一份80页的VC++, VC#, VB. ...

最新文章

  1. ubuntu/linuxmint如何添加和删除PPA源
  2. DeepLearning:windows环境下C++环境实现Tensorflow编译部署
  3. 1、python的基础
  4. 数据结构--树形结构
  5. java B2B2C Springcloud电子商城系统-Spring Cloud常见问题与总结(四)
  6. 表达式ya是不合法的c语言标识符,《C语言程序设计》试题3及答案
  7. 细数阿里云服务器的十二种典型应用场景
  8. 清理linux 服务器的命令行,使用Linux上的Magic SysRq键修复冻结的X服务器,清理重新启动并运行其他低级命令 | MOS86...
  9. 演练 可以飞可以喷火的人 java
  10. 如何将matlab转成jav,Matlab将GUI转化为exe文件,并且进行发布
  11. linux wqy字体安装,给openSUSE安装文泉驿字体
  12. 三维激光雷达点云处理分类
  13. 联想Y7000安装ubuntu1604 双系统
  14. SQLite3介绍及SQL语句详解(SQLite一)
  15. 英特尔核心显卡控制面板设置自定义分辨率
  16. python模拟访问js_百度统计原理分析-利用PHP/Python实现模拟访问
  17. 黑马SpringBoot --基础篇
  18. 邮箱激活功能,前台用户注册后需要登录邮箱激活账号才能够登录的,请简述激活过程,重点是安全性
  19. 黑马程序员——C语言学习——概述
  20. 特斯拉、软件与颠覆,下一家震惊世界的科技公司会是谁?

热门文章

  1. LeetCode 6060. 找到最接近 0 的数字
  2. LeetCode 1599. 经营摩天轮的最大利润(模拟)
  3. 使用swiper_关于使用swiper制作web轮播图
  4. crc java_java实现CRC16 MODBUS校验算法
  5. android 脚本引擎,GitHub - PassByYou888/zExpression: 脚本与编译器内部的语法引擎内核,也是一种op内核,zExpression可以轻松实现自己的脚本引擎...
  6. linux c代码出现段错误,Linux下段错误(C语言)
  7. 小案例:编写立方体六个面,合成一张全景图后端
  8. Flink的Table API 与SQL的流处理
  9. java私有表示标识_java里面的标识符、关键字和类型
  10. 一款三搭_冬日穿搭指南!照着这八条万能公式穿,让你不冻还有型_