- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要从我的 Controller 之一的构造函数中访问 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
是同一个 Controller 的属性,它看起来像这样:
public ApplicationUserManager UserManager
{
get
{
if (_userManager == null)
{
_userManager = HttpContext.GetOwinContext()
.GetUserManager<ApplicationUserManager>();
}
return _userManager;
}
private set
{
_userManager = value;
}
}
HttpContext
,这是
Controller
的属性类,类型为
System.Web.HttpContextBase
并且不是
System.Web.HttpContext
实例,为空。
OwinContext
的引用。直接使用
System.Web.HttpContext.Current
属性(property)。但是,该属性的类型是
System.Web.HttpContext
其中
GetOwinContext
是类型
System.Web.HttpContextBase
上的扩展方法我看到这两个类彼此没有任何关系。
System.Web.HttpContext.Current
获得至
System.Web.HttpContextBase
?
最佳答案
就在这里:
HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current);
HttpContext
在构建 Controller 期间,属性始终为空。您可以在
Controller.Initialize
method 中(和之后)安全地使用它.
Initializes data that might not be available when the constructor is called.
关于asp.net - 将 System.Web.HttpContext.Current 转换为 System.Web.HttpContextBase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237129/
使用以下代码(使用 Moq 4.0.10501.6): HomeController controller = new HomeController(); ActionResult result =
我有一个 ASP.NET 应用程序,它需要为 View 设置多个布局。有一个正常的 Web 布局,以及一个没有依赖项的自包含页面的“静态”布局(用作文档管理系统中的显示模板)。 让 View 切换其布
我想模拟 HttpContext 的 User 属性。我正在使用 Scott Hanselman 的 MVCHelper 类和 RhinoMocks。 我有一个包含代码的单元测试,如下所示: ...
public string GetCartId(HttpContextBase context) { if (context.Session[CartSessionKey] == null)
我最近开始使用 TDD 和依赖注入(inject)。这是一个很大的心态变化,我仍在努力将所有部分拼凑在一起。 我有一个用于所有业务逻辑的服务层,以使 Controller 尽可能薄。我的服务中的某些服
我正在尝试创建自己的自定义 HTTP 上下文: CustomHttpContext : HttpContextBase { public override HttpRequestBase Re
代码 using System.Web; abstract class CookieHandler { public CookieHandler(string domain) {
我正在开发一个 ASP.NET MVC 应用程序,并试图针对 Controller 操作编写一些单元测试,其中一些操作操作 HttpContext 上的属性,例如 Session、Request.Co
我遇到了一个非常奇怪的错误,我只在一些相当广泛的谷歌搜索中发现了几次。我正在创建一个授权属性以添加到我在类库中的 ASP.NET MVC 项目中的操作中。我通过 NuGet 安装了 ASP 包,并且可
我有一个单元测试装置,我试图在 ASP.NET MVC Controller 上测试 ControllerAction,该 Controller 用于 Web 应用程序上的成员函数。我正在尝试模拟 H
我需要掌握当前HttpContextBase在 ASP.NET WEB API 中 AuthorizeAttribute . 我如何从 HttpActionContext 获得该信息覆盖的 IsAut
我正在努力实现自定义的 AuthorizeAttribute。 AuthorizeCore 覆盖接受 HttpContextBase。如果用户没有正确的角色,那么我想抛出一个错误。我找到了一些代码,我
两者都有 Request 和 Response 属性,但我无法编写一个采用 HttpContext 或 HttpContextBase 的方法。在某些地方,一个或另一个都可用,所以我需要同时处理这两个
我正在开发一个 C# ASP.NET MVC 应用程序,它使用 Windows 模式身份验证在 Intranet 上运行,具有以下 Web.config 部分文件:
我目前正在尝试使用 Microsoft Fakes Framework 编写一些单元测试。对于特定的单元测试,我必须模拟 Controller.User 属性。所以我为 System.Web 添加了一
我试图在一个静态类上构建一个静态属性,该属性基本上会返回一个 cookie 值,以在我的 MVC 站点(MVC 3,如果重要的话)中使用。像这样的东西: public static class Sha
我基于以下示例实现了一个 session 助手来保存和检索 session 变量:(我尽量减少 session 变量的使用) Stackoverflow question 我在 Visual Stud
这是一个运行 .Net 3.5 的 Asp.net 应用程序(不是 MVC) 我这样做了: protected void Application_Start(object sender, Event
我需要从我的 Controller 之一的构造函数中访问 OwinContext,如下所示: protected SEMController() { var currentUserIsAdmi
我在 NinjectWebCommon.RegisterServices 方法中创建了 HttpContextBase 的绑定(bind),但是当我尝试在 Controller 或服务中引用它时,我收
我是一名优秀的程序员,十分优秀!