Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 939
Sum None Avatar asked Aug 07 '15 20:08

Sum None


4 Answers

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>();
like image 100
Sam FarajpourGhamari Avatar answered Oct 19 '22 14:10

Sam FarajpourGhamari


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

like image 44
Spynol Avatar answered Oct 19 '22 14:10

Spynol


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.

like image 43
Rajsanthosh Sreenivasan Avatar answered Oct 19 '22 14:10

Rajsanthosh Sreenivasan


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

like image 39
Gobind Gupta Avatar answered Oct 19 '22 14:10

Gobind Gupta