20

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...

Community
  • 1
  • 1
Sum None
  • 2,164
  • 3
  • 27
  • 32
  • 2
    from what i've seen controllers have their own HttpContext `Controller.HttpContext` and there is a `System.Web.HttpContext` Controller.HttpContext has GetOwinContext().. both have session too but think for one second they're the same either :) – JamieD77 Aug 07 '15 at 20:35
  • possible duplicate of [ASP.Net Identity - HttpContext has no extension method for GetOwinContext](http://stackoverflow.com/questions/21148209/asp-net-identity-httpcontext-has-no-extension-method-for-getowincontext) – NightOwl888 Aug 08 '15 at 01:45
  • 1
    I blame [naming conventions](https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members) for this one, I personally never liked having property named same as the type. Also the 3rd issue stems from the extension missing if it isn't clear (see @NightOwl888 's link above) – NSGaga-mostly-inactive Dec 29 '17 at 13:44

4 Answers4

39

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>();
Sam FarajpourGhamari
  • 14,601
  • 4
  • 52
  • 56
  • I saw the `Current` over here in this SO question ([here](http://stackoverflow.com/questions/19431820/system-web-httpcontextbase-does-not-contain-a-definition-for-current-mvc-4-wi)). 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. – Sum None Aug 08 '15 at 11:35
20

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

Spynol
  • 229
  • 3
  • 6
11

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.

  • Worked in my case; I had been cherry picking the Owin packages and had accidentally left this one out! Thanks! – gciochina Nov 05 '17 at 13:10
3

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

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28
Gobind Gupta
  • 203
  • 3
  • 13