栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在测试Init方法中模拟HttpContext.Current

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在测试Init方法中模拟HttpContext.Current

HttpContext.Current
返回的实例
System.Web.HttpContext
,该实例不扩展
System.Web.HttpContextbase
HttpContextbase
后来添加,以解决
HttpContext
难以嘲笑的问题。这两个类基本上是不相关的(
HttpContextWrapper
用作它们之间的适配器)。

幸运的是,

HttpContext
它本身就是可伪造的,足以替换
IPrincipal
(User)和
IIdentity

即使在控制台应用程序中,以下代码也会按预期运行:

HttpContext.Current = new HttpContext(    new HttpRequest("", "http://tempuri.org", ""),    new HttpResponse(new StringWriter())    );// User is logged inHttpContext.Current.User = new GenericPrincipal(    new GenericIdentity("username"),    new string[0]    );// User is logged outHttpContext.Current.User = new GenericPrincipal(    new GenericIdentity(String.Empty),    new string[0]    );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433115.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号