'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery

32,870

Solution 1

When you are writing System.Web.HttpContext actually you are pointing to a class. But when you are writing HttpContext inside of a controller you are using a property named HttpContext which returns an object of the HttpContext class. You could also reach the same object by calling the System.Web.HttpContext.Current static property. Therefore you could write:

System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

Solution 2

Just go to NuGet Packages Manager of the project, search and install this Microsoft.Owin.Host.SystemWeb and you can use using System.Web; to enable GetOwinContext() method

Solution 3

DSR's answer in this link worked for me

Working in MVC 5 Identity for email verification

It seems you have a missing NuGet package called 'Microsoft.Owin.Host.SystemWeb'. Install this package from NuGet or use the package manager console to install said package.

Install-Package Microsoft.Owin.Host.SystemWeb

Hope this helps.

Solution 4

Thanks it works in my case when i added Microsoft.Owin.Host.SystemWeb from NugetpackageManager. It provides the extension method GetOwinContext() to the HttpContext.

Share:
32,870
Sum None
Author by

Sum None

Competent semi-retired Sr. systems and security engineer. Programming/development... not so much.

Updated on July 05, 2022

Comments

  • Sum None
    Sum None almost 2 years

    I realize this question might seem trivial to some, but it's these types of things that I find myself fighting with quite a bit and I just want to make sense of it all despite that seeming to be a losing battle in .net (for me anyway).

    So, if I do the following:

        using System.Web;
    ...
    ApplicationUser user = System.Web.HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.User.Identity.GetUserId());
    

    That produces the error in the title and a red GetOwinContext() and the error Cannot resolve symbol 'GetOwinContext()'

    However, if I do the following (remove System.Web from in front of HttpContext), it works as expected (or at least no errors):

    using System.Web;
        ...
        ApplicationUser user = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.User.Identity.GetUserId());
    

    However, if I do this (same line that's working with using System.Web commented out):

        //using System.Web;
                ...
    ApplicationUser user = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.User.Identity.GetUserId());
    

    It produces the same red GetOwinContext() and the error Cannot resolve symbol 'GetOwinContext()'

    If I google HttpContext I can only find that it stems from System.Web.

    So, the question is why can't I use the full syntax like in the first example above? (Also mentioned in the answer here with the highest votes: ASP.NET MVC 5 - Identity. How to get current ApplicationUser)

    UPDATE (to address duplicate question reply): While there is an answer on that question that may come to the same conclusion, I don't really understand how this is a duplicate question. Try to think of it from a newbie perspective and dissecting all the smoke and mirrors that is .Net. I have never tried to learn something so convoluted in my life as .Net and sometimes you have to look at things from many different angles.

    I actually saw that question and one other regarding using Current, but neither struck me as 1) being the answer I was looking for (at the time) 2) more importantly, why it's behaving like that. Sam's answer is perfect, although a bit over my head. But, at least now, I can go research what it all means...

  • Sum None
    Sum None almost 9 years
    I saw the Current over here in this SO question (here). Unfortunately, I lost my undos after my computer crashed last night, but I think I tried to put it after GetOwinContext() like a dope. I think your answer is a little over my head at the moment, but I can at least research now and digest... Thank you.
  • gciochina
    gciochina over 6 years
    Worked in my case; I had been cherry picking the Owin packages and had accidentally left this one out! Thanks!