刚刚接触.net web端的朋友都会被Session坑过,莫名其妙的不能读取Session数据,后来知道原来有IRequiresSessionState这个接口,不继承的就不能读取Session里面的数据,知道这个以后呢,也不清楚里面具体是如何实现的。对此一直不甘心,于是查了各方面的资料终于模拟出来了。

  在一般处理程序(ashx文件)里面有个一个(HttpContext Context),F12进入HttpContext 类你面你会发现它应该是用了单例的模式,里面有个 public static HttpContext Current { get; set; },应该是确定程序只有一个上下文。接下来可以找到public HttpSessionState Session { get; },这就是我们需要读取Session。
废话少说,首先说明用到了反射。我们来介绍下Type 类中的Type IsAssignableFrom(Type c);方法。假设A类继承了B接口,  Type a = typeof(A);  Type b = typeof(B); 那么 a. IsAssignableFrom(b)的值为ture;这个可以判断类是否继承了IRequiresSessionState。这是第一步。
  第二步就是找到当前访问Session的类。这个就要用到StackTrace类,从名字上来看这个类是用来跟踪代码的。这里面要用到StackTrace 的GetFrame(index)方法和GetMethod(); 。GetFrame(index)这个是从调用的最里层往外层遍历,它的返回值也是StackTrace 。是GetMethod() 返回值是MethodBase,而MethodBase的ReflectedType属性可以得到当前类的Type。
  原理都在上面的,下面的代码是模拟过程。
  

using System;
using System.Diagnostics;
using System.Reflection;
using System.Web.SessionState; namespace Ztest
{
public class Program: IRequiresSessionState
{
public static void Main(string[] args)
{
try
{
if (Test.Current.session == null)
{
Console.WriteLine("没有继承IRequiresSessionState");
}
else
{
Console.WriteLine(Test.Current.session);
}
}
catch (Exception ex)
{
}
Console.ReadLine();
}
}
public class Test
{
private Test()
{
Type basetype = typeof(IRequiresSessionState);
StackTrace trace = new StackTrace();
int i = 0;
Type type;
while (true)
{
///找到外层第一个调用类
MethodBase methodName = trace.GetFrame(i).GetMethod();
type = methodName.ReflectedType;
if (type != typeof(Test))
{
break;
}
i++;
} Boolean key = basetype.IsAssignableFrom(type);
if (key)
{
session = _m;
}
else
{
session = null;
}
}
private static Test _Current;
private string _m = "当前类实现了IRequiresSessionState";
/// <summary>
/// 模拟session
/// </summary>
public Object session { get; set; }
public static Test Current
{
get
{
return get();
}
set
{
Current = value;
}
}
private static Test get()
{
if (_Current == null)
{
_Current = new Test(); }
return _Current;
}
} }

  

 

IRequiresSessionState接口控制的更多相关文章

  1. HttpHandler和ashx要实现IRequiresSessionState接口才能访问Session信息(转载)

    通常我们经常,通过session判定用户是否登录.还有一些临时的.重要的数据也尝尝存放在Session中. 在页面我们很容易的得到Session的值,但在类中就会遇到一些问题.也知道通过下面的方法得到 ...

  2. CAN-bus接口控制实验

    CAN-bus接口控制实验 2016-04-12 20:38:41来源: eefocus 关键字:CAN  bus  接口控制   收藏 评论(0) 分享到 微博 QQ 微信 LinkedIn 一.实 ...

  3. sama5 kio接口控制

    //example #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <str ...

  4. 在ashx文件中制作验证码(使用session要继承IRequiresSessionState)

    必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写! System.Web.SessionState的一些接口 IRea ...

  5. Asp.net下使用HttpModule模拟Filter,实现权限控制

    在asp.net中,我们为了防止用户直接从Url中访问指定的页面而绕过登录验证,需要给每个页面加上验证,或者是在模板页中加上验证.如果说项目比较大的话,添加验证是一件令人抓狂的事情,本次,我就跟大家分 ...

  6. 【转载】关于Alipay支付宝接口(Java版)

    转载自:http://blog.163.com/lai_chao/blog/static/70340789201412724619514/ 1.alipay 双功能支付简介 2.alipay 提交支付 ...

  7. 在一般处理文件中访问Session需要添加IRequiresSessionState(转载)

    原文:http://blog.csdn.net/cdsnaspnet/article/details/5695625s 通常我们经常,通过session判定用户是否登录.还有一些临时的.重要的数据也尝 ...

  8. (二)再议MII、RMII、GMII接口

    概述:         MII (Media Independent Interface(介质无关接口)或称为媒体独立接口,它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口和一个MAC ...

  9. 61.MII、RMII、GMII接口的详细介绍

    概述: MII (Media Independent Interface(介质无关接口)或称为媒体独立接口,它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口和一个MAC和PHY之间的管 ...

随机推荐

  1. 基于InstallShield2013LimitedEdition的安装包制作

    在VS2012之前,我们做安装包一般都是使用VS自带的安装包制作工具来创建安装包的,VS2012.VS2013以后,微软把这个去掉,集成使用了InstallShield进行安装包的制作了,虽然思路差不 ...

  2. php Zend Opcache,xcache,eAccelerator缓存优化详解(具体根据个人需要选择其一即可,功能都一样切勿重复选择)

    载入 XCache 模块 引用 ;; 安装成 zend extension (推荐), 路径一般是 "$extension_dir/xcache.so" zend_extensio ...

  3. HTML中使图片居中显示

    注:imageId为图片id<style type="text/css"> #imageId{ display:block; position:relative; ma ...

  4. ajax请求成功后打开新开窗口(window.open())被拦截的解决方法

    问题:今天在做项目时需要在ajax请求成功后打开一个新的窗口,此时遇到浏览拦截了新窗口的问题,尝试在ajax 回调函数中模拟执行 click 或者 submit 等用户行为(trigger('clic ...

  5. codegate-quals-2013-vuln100

    最近想多看看题目积累些经验, -------- 程序分析 64位,保护措施都没开 gdb-peda$ checksec CANARY : disabled FORTIFY : disabled NX ...

  6. JS--轻松设置获取表单数据

    接触过Angularjs的都知道,ng支持双向绑定,我们可以轻轻松松的通过ngModel将我们的值绑定到界面,当修改了值提交表单的时候不需要再重新通过ID去重新抓取输入框信息了.那对于我们开发前台网站 ...

  7. Linux shell redirect

    Learn much from here Learn much from here

  8. [LeetCode] Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  9. java 分页功能

    1.分页工具类 package com.bw.shop.util; import java.util.List; import com.sun.org.apache.regexp.internal.r ...

  10. 阿里云VPS服务器,ROS内网穿透

    Aliyun Windows Server 2008 R2中建立vpn服务器,ros中使用pptp拨号连接 2.在Aliyun服务器中,修改hosts,将内网分配的ip映射到指定的域名,在Aliyun ...