Can't find Request.GetOwinContext

后端 未结 13 1254
北荒
北荒 2020-12-04 22:47

I have been searching for an hour trying to figure out why this isn\'t working.

I have a ASP.Net MVC 5 application with a WebAPI. I am trying to get Request.GetOwin

相关标签:
13条回答
  • 2020-12-04 23:37

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

    HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
    
    0 讨论(0)
  • 2020-12-04 23:39

    The GetOwinContext extension method is in the System.Web.Http.Owin dll which needs to be downloaded as a nuget package (The nuget package name is Microsoft.AspNet.WebApi.Owin)

    Install-Package Microsoft.AspNet.WebApi.Owin
    

    See msdn here: 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, the method is still part of the System.Net.Http namespace, so the using definitions you have should be fine.

    EDIT

    Okay, to clear up some confusion: If you are using an ApiController (i.e MyController : ApiController) you will require 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 have the same naming conventions. So an extension method in one does not apply to the other. Same for a lot of the action filters etc.

    0 讨论(0)
  • 2020-12-04 23:39

    None of these worked for me. I had to compare Nuget packages with one that was created with Identity and I found this Nuget package missing which when added fixed the issue for me

    Microsoft.Owin.Host.SystemWeb
    

    Apparently you need it to run OWIN on IIS using the ASP.NET request pipeline (read you're screwed without it!)

    0 讨论(0)
  • 2020-12-04 23:39

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

    So be careful to not call this function from within any awaited function.

    0 讨论(0)
  • 2020-12-04 23:39

    I was missing package: Microsoft.AspNet.WebApi.Owin for ApiControllers. Hope this helps!

    0 讨论(0)
  • 2020-12-04 23:48

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

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

    it works in Identity 2.0

    0 讨论(0)
提交回复
热议问题