asp.net – 将System.Web.HttpContext.Current转换为System.Web.
我需要从我的一个控制器的构造函数中访问OwinContext,如下所示:
protected SEMController() { var currentUserIsAdmin = false; var currentUserName = System.Web.HttpContext.Current.User?.Identity?.Name; if (!string.IsNullOrEmpty(currentUserName)) { var user = UserManager.Users .SingleOrDefault(u => u.UserName.Equals(currentUserName,StringComparison.InvariantCultureIgnoreCase)); if (user != null) { currentUserIsAdmin = UserManager.IsInRole(user.Id,UserType.Admin); } } TempData["CurrentUserIsAdmin"] = currentUserIsAdmin; } 其中UserManager是同一个控制器的属性,它看起来像这样: public ApplicationUserManager UserManager { get { if (_userManager == null) { _userManager = HttpContext.GetOwinContext() .GetUserManager<ApplicationUserManager>(); } return _userManager; } private set { _userManager = value; } } 但是,当代码在ctor中时,HttpContext是Controller类的一个属性,类型为System.Web.HttpContextBase,而不是System.Web.HttpContext实例,则为null. 但是,无论如何,ASP.NET框架只是将信息从一个地方复制到另一个地方,他们将在稍后的某个时间点获得的信息将是相同的. 所以,我想知道是否可以通过直接使用System.Web.HttpContext.Current属性来获取对OwinContext的引用.但是,该属性的类型为System.Web.HttpContext,其中GetOwinContext是System.Web.HttpContextBase类型的扩展方法,我发现这两个类彼此无关. 所以,我想知道是否有办法从System.Web.HttpContext.Current到System.Web.HttpContextBase? 解决方法
就在这里:
HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current); 是的,在构造控制器期间,HttpContext始终为null.您可以在
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – IE超时后重新发送请求?
- asp.net-mvc – 将MvcSiteMapProvider与属性和属性路由一起
- asp.net-mvc-3 – MVC 3 jQuery对话框的客户端验证
- asp.net-mvc-4 – 如何在asp.net mvc4中将OpenID迁移到OAut
- asp.net-mvc – ASP.NET MVC获取下拉列表值
- IIS会话超时与ASP.NET会话超时
- 如何使用asp.net mvc和razor模板引擎从c#中获取路由名称的U
- asp.net-mvc – 我可以使用Visual Studio的快速版本制作ASP
- 在ASP.NET中动态设置主题
- asp.net-core – 将参数传递给ASP.NET MVC 6中的需求/策略
- asp.net – 启用Application Insights会使Web应用
- Asp.net Identity 2.0更新用户
- asp.net-mvc-3 – ASP.NET MVC 3中ViewBag.Title
- asp.net-mvc – 当可观察数组为空时如何显示无数
- asp.net – 正则表达式匹配10-14位数
- asp.net – 如何让Html Link(锚点)回发像LinkBut
- 使用ASP.NET Web API 2.1配置依赖注入
- asp.net – 使用web.config存储设置的替代方法(适
- 将asp mvc3不显眼验证与jquery验证插件混合使用
- asp.net-mvc – ASP.NET MVC单元测试控制器与Htt