I need to access the OwinContext from within a constructor of one of my controllers, like so:
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;
}
where the UserManager is a property of the same controller and it looks like this:
public ApplicationUserManager UserManager
{
get
{
if (_userManager == null)
{
_userManager = HttpContext.GetOwinContext()
.GetUserManager<ApplicationUserManager>();
}
return _userManager;
}
private set
{
_userManager = value;
}
}
However, at the time the code is in the ctor, the HttpContext, which is a property of the Controller class and is of type System.Web.HttpContextBase and is not a System.Web.HttpContext instance, is null.
But since anyway the ASP.NET framework simply copies information from one place to another, the information they will have , at some point later in time, will be the same.
So, I was wondering if I could get a reference to the OwinContext by directly using the System.Web.HttpContext.Current property. However, that property is of type System.Web.HttpContext where as the GetOwinContext is an extension method on the type System.Web.HttpContextBase and I see that these two classes are no way related to each other.
So, I was wondering if there was a way to get from System.Web.HttpContext.Current to System.Web.HttpContextBase?