Sum None
Sum None

Reputation: 2279

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

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

Upvotes: 21

Views: 42452

Answers (4)

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.

Upvotes: 11

Sam FarajpourGhamari
Sam FarajpourGhamari

Reputation: 14741

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>();

Upvotes: 40

Gobind Gupta
Gobind Gupta

Reputation: 223

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

Upvotes: 4

The Duke
The Duke

Reputation: 239

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

Upvotes: 21

Related Questions