Cannot find Request.GetOwinContext

I searched for an hour trying to figure out why this is not working.

I have an ASP.Net MVC 5 application with WebAPI. I am trying to get Request.GetOwinContext (). Authentication, however, I cannot find how to enable GetOwinContext. Here is my code:

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web; using System.Web.Mvc; using System.Web.Security; using TaskPro.Models; namespace TaskPro.Controllers.api { public class AccountController : ApiController { [HttpPost] [AllowAnonymous] public ReturnStatus Login(LoginViewModel model) { if (ModelState.IsValid) { var ctx = Request.GetOwinContext(); // <-- Can't find this return ReturnStatus.ReturnStatusSuccess(); } return base.ReturnStatusErrorsFromModelState(ModelState); } } } 

From what I read, it should be part of System.Net.Http, but I have included this and it still does not allow. Ctrl-Space does not give me any intellisense options.

What am I missing here?

+71
c # asp.net-web-api owin
Mar 23 '14 at 23:33
source share
11 answers

The GetOwinContext extension GetOwinContext is located in the System.Web.Http.Owin dll, which must be loaded as the nuget package (the nuget package name is Microsoft.AspNet.WebApi.Owin)

 Install-Package Microsoft.AspNet.WebApi.Owin 

See here msdn: http://msdn.microsoft.com/en-us/library/system.net.http.owinhttprequestmessageextensions.getowincontext(v=vs.118).aspx

Nuget package here: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Owin

However, this method is still part of the System.Net.Http namespace, so the using definitions you have must be exact.

EDIT

Good to eliminate some confusion: if you are using ApiController ( MyController : ApiController ), you will need the Microsoft.AspNet.WebApi.Owin package.

If you are using a regular Mvc controller (i.e. MyController : Controller ), you will need the Microsoft.Owin.Host.SystemWeb package.

In MVC 5, the pipelines for Api and regular MVC were very different, but often had the same naming convention. Therefore, the extension method in one does not apply to the other. The same goes for many action filters, etc.

+118
Mar 24 '14 at 0:09
source share

None of them worked for me. I had to compare Nuget packages with the one that was created using Identity, and I found that this Nuget package is missing when adding a problem for me

 Microsoft.Owin.Host.SystemWeb 

Apparently you need to run OWIN in IIS using the ASP.NET request pipeline (read that you are screwed without it!)

+42
Jun 06 '14 at 13:15
source share

In the WEB API, you can get the link using the following:

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

it works in identity 2.0

+35
Apr 25 '14 at 8:18
source share

You may need to add the Microsoft.Owin.Host.SystemWeb NuGet package to do this:

 HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
+19
Sep 06 '14 at 23:30
source share

It took me forever to find a simple answer: but what I did was use the Get extension of one instance of IOwinContext created at startup. It happened like this:

 private readonly IOwinContext _iOwinContext = HttpContext.Current.GetOwinContext(); public ApplicationUserManager UserManager { get { return _userManager ?? _iOwinContext.Get<ApplicationUserManager>() ; } private set { _userManager = value; } } 
+2
Jan 13 '15 at 3:14
source share

I have this problem and I download an additional package from nuget to solve my problem (run the following command in the package manager console) Install the Microsoft.Owin.Host.SystemWeb package

+2
Apr 09 '17 at 23:28
source share

In my case, I need to add

 using Microsoft.AspNet.Identity.Owin; 

to resolve GetOwinContext and then GetUserManager on the next line.

 Request.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
+2
May 31 '17 at 7:19 a.m.
source share

The GetOwinContext() extension method was not found in threads other than the GUI thread.

Therefore, be careful not to call this function from any await ed function.

+1
Nov 27 '15 at 15:11
source share

After looking at the default ASP.NET project, I found that I need to include it in my application launch:

 // Enable the application to use a cookie to store information for the signed in user // and to use a cookie to temporarily store information about a user logging in // with a third party login provider app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);` 
+1
Mar 27 '16 at 23:20
source share

I missed the package: Microsoft.AspNet.WebApi.Owin for ApiControllers. Hope this helps!

+1
Jul 28 '16 at 12:26
source share

I had the same problem and it was solved using a private method: private IAuthenticationManager AuthenticationManager { get { return HttpContext.Current.GetOwinContext().Authentication; } } private IAuthenticationManager AuthenticationManager { get { return HttpContext.Current.GetOwinContext().Authentication; } }

It worked smoothly for me.

+1
Jan 23 '17 at 21:41
source share



All Articles