注:

在ashx文件中使用Session

首先添加引用

using System.Web.SessionState;

实现接口

public class XXXX: IHttpHandler
==>
public class XXXX: IHttpHandler, IRequiresSessionState

使用的时候需要通过HttpContext对象调用,如

public void ProcessRequest (HttpContext hc) {
string code = hc.Session["Cold"].ToString();
}

为了实现负载均衡,Session的共享是必须要面对的事情

同一个用户,先后发出的多次请求很可能不是同一台服务器处理的, 那么默认的进程中 Session存储方式 在这多次的处理中就无法共享Session。

解决这个问题,要有两个事情要面对:

  • Session在服务器端是以字典的形式存储的,而获取Session的键就是Cookie(ASP.NET_SessionId),每个服务器都会创建一个SessionId来与客户端的保持联系,所以如何让多个服务器使用同一个SessionId是一个问题。
  • 原来Session是存到每个服务器的对应进程中的,现在要共享Session,就需要在统一的地点存储这些内容。

解决方案:

第一个问题,处理方法就是每个服务器都设置Cookie(ASP.NET_SessionId)的Domain,这样多个服务器就可以使用同一个SessionId了。

注:请求到达后台,如果没有发现同域下的SessionId,则会重新生成一个SessionId,并覆盖到客户端的Cookies中。

using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Web.SessionState; namespace Entity
{
public class SessionProviderHttpModule : IHttpModule
{
private string m_RootDomain = string.Empty; #region IHttpModule Members public void Dispose()
{ } public void Init(HttpApplication context)
{
m_RootDomain = ConfigurationManager.AppSettings["RootDomain"]; Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic); if (uriField == null)
throw new ArgumentException("UriField was not found"); uriField.SetValue(null, m_RootDomain); context.EndRequest += new System.EventHandler(context_EndRequest);
} void context_EndRequest(object sender, System.EventArgs e)
{
HttpApplication app = sender as HttpApplication;
for (int i = ; i < app.Context.Response.Cookies.Count; i++)
{
if (app.Context.Response.Cookies[i].Name == "ASP.NET_SessionId")
{
app.Context.Response.Cookies[i].Domain = m_RootDomain;
}
}
} #endregion
}
}

然后还要将这个httpModule注入到项目中,即在<system.webServer>或<system.web>的modules中添加一个节点。

  <system.webServer>
<modules>
<add name="CrossDomainCookieModule" type="Entity.SessionProviderHttpModule,Entity" />
</modules>
</system.webServer>

第二个问题的解决方法有多种,如状态服务器、SQL Server、分布式缓存(Memcache)。

具体细节参考另一篇文章 ASP.NET Session的保存


原文:http://www.cnblogs.com/lori/p/3723714.html

ASP.NET Session的共享的更多相关文章

  1. asp.net session分布式共享解决方案

    Session共享是分布式系统设计时必须考虑的一个重要的点.相比较java中的session共享解决方案,.net中的解决方案还是比较少,MemcachedSessionProvider类库是比较优秀 ...

  2. ASP.NET Session 详解

    阅读本文章之前的准备 阅读本文章前,需要读者对以下知识有所了解.否则,阅读过程中会在相应的内容上遇到不同程度的问题. 懂得ASP/ASP.NET编程 了解ASP/ASP.NET的Session模型 了 ...

  3. [转]ASP.NET Session 详解

    来源:http://www.webshu.net/jiaocheng/programme/ASPNET/200606/1381.html 阅读本文章之前的准备 阅读本文章前,需要读者对以下知识有所了解 ...

  4. Session分布式共享 = Session + Redis + Nginx

    一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我就简单的介绍一下. Session:在计算机中,尤其是在网络应用中,称为"会话控制 ...

  5. 详解Session分布式共享(.NET CORE版)

    一.前言&回顾 在上篇文章Session分布式共享 = Session + Redis + Nginx中,好多同学留言问了我好多问题,其中印象深刻的有:nginx挂了怎么办?采用Redis的S ...

  6. 什么是Session分布式共享

    在了解session分布式共享之前先来了解Session.Redis和Nginx的相关知识. 一.Session相关知识 1.Session 介绍 Session在网络应用中,称为“会话控制”. 每个 ...

  7. Session分布式共享 = Session + Redis + Nginx(转)

    出处:http://www.cnblogs.com/newP/p/6518918.html 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我 ...

  8. ASP.NET Session原理及处理方法

    session是怎么存储,提取的 1.在服务器端有一个session池,用来存储每个用户提交session中的数据,Session对于每一个客户端(或者说浏览器实例)是“人手一份”,用户首次与Web服 ...

  9. ASP.net session丢失

    ASP.NET Session的实现: asp.net的Session是基于HttpModule技术做的,HttpModule可以在请求被处理之前,对请求进行状态控制,由于Session本身就是用来做 ...

随机推荐

  1. jquery.idTabs使用方法

    idTabs是基于Jquery编写封装的一个插件,主要用于实现选项卡功能,它操作简单,只需到官网:http://www.sunsean.com/idTabs/下载插件JS脚本文件,并引用到网站中即可 ...

  2. mysql 查看当前登陆用户匹配原则及权限user()与current_user()

    Mysql在进行登陆时,会去匹配mysql库中的user表,并赋予相应的权限,但是怎么知道我们当时的登陆的用户名及相应的权限呢? 在Mysql中,有两个函数,一个是user(),一个是current_ ...

  3. ASP.NET获取IP的6种方法

    服务端: //方法一 HttpContext.Current.Request.UserHostAddress; //方法二 HttpContext.Current.Request.ServerVari ...

  4. 关于重装系统与Visual Studio 2015

    开始入坑  ... 9.25左右,由于本人电脑上同时安装了社区版vs2013与社区版vs2015,.无奈天公不作美,vs2015我每次一点击右上角的“登录”,马上就白屏,并弹窗提示,停止运行(忘了截屏 ...

  5. 适用于jquery1.11.1的ajaxfileupload.js

    ajaxfileupload源码 解决上传成功不走success的问题 解决高版本jquery兼容性问题 jQuery.extend({ createUploadIframe: function(id ...

  6. C#调用windows api示例

    这是运行结果: Api函数是构筑Windws应用程序的基石,每一种Windows应用程序开发工具,它提 供的底层函数都间接或直接地调用了Windows API函数,同时为了实现功能扩 展,一般也都提供 ...

  7. sphinx配置文件sphinx.conf参数详细说明

    sphinx配置文件sphinx.conf参数详细说明 sphinx.conf各个参数详细说明 # # Sphinx configuration file sample # # WARNING! Wh ...

  8. ActiveReports 报表应用教程 (6)---分组报表

    在 ActiveReports 中可以设置单级分组.嵌套分组,同时,还可以使用表格.列表以及矩阵等数据区域控件对数据源进行分组操作.分组报表在商业报表系统中应用不胜枚举,客户信息归类统计表.商品分类统 ...

  9. LeetCode132:Palindrome Partitioning II

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  10. NYOJ:题目529 flip

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=529 由于此题槽点太多,所以没忍住...吐槽Time: 看到这题通过率出奇的高然后愉快的进 ...