Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert System.Web.HttpContext.Current to System.Web.HttpContextBase

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?

like image 643
Water Cooler v2 Avatar asked Jul 07 '16 03:07

Water Cooler v2


People also ask

What is System Web HttpContext current application?

HttpContext. Current. Application is simply a reference to the static global HttpApplicationState object in . NET for your Web Application, of which there should be one global instance per web application. By storing data there, you provide fast, thread-safe access to your global variables.

How can get HttpContext current in asp net core?

In ASP.NET Core, if we need to access the HttpContext in service, we can do so with the help of IHttpContextAccessor interface and its default implementation of HttpContextAccessor. It's only necessary to add this dependency if we want to access HttpContext in service.

What is HttpContext in .NET core?

HttpContext encapsulates all information about an individual HTTP request and response. An HttpContext instance is initialized when an HTTP request is received. The HttpContext instance is accessible by middleware and app frameworks such as Web API controllers, Razor Pages, SignalR, gRPC, and more.


1 Answers

Yes, there is:

HttpContextBase httpContext = new HttpContextWrapper(HttpContext.Current);

And yes, the HttpContext property is always null during the construction of controllers. You can use it safely in (and after) the Controller.Initialize method.

Initializes data that might not be available when the constructor is called.

like image 127
NightOwl888 Avatar answered Sep 26 '22 00:09

NightOwl888