Can't find Request.GetOwinContext
C#asp.net Web-ApiOwinC# Problem Overview
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.GetOwinContext().Authentication, however I can't seem to find how to include 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've read, it should be part of the System.Net.Http, but I've included that and it still isn't resolving. Ctrl-Space doesn't give me any intellisense options either.
What am I missing here?
C# Solutions
Solution 1 - C#
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.
Solution 2 - C#
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!)
Solution 3 - C#
In WEB API, you can get the reference using the following:
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
it works in Identity 2.0
Solution 4 - C#
You may need to add the NuGet package Microsoft.Owin.Host.SystemWeb
in order to do this:
HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
Solution 5 - C#
In my case I need to add
using Microsoft.AspNet.Identity.Owin;
for resolving GetOwinContext and then GetUserManager in following line.
Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
Solution 6 - C#
I have this problem and I download extra package from nuget to solve my problem,(run following command in Package Manager Console) Install-Package Microsoft.Owin.Host.SystemWeb
Solution 7 - C#
This took me forever to find a simple answer: but what I did was use the Get extension of the single instance of the IOwinContext that was instantiated in the startup. So it came out like this:
private readonly IOwinContext _iOwinContext = HttpContext.Current.GetOwinContext();
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? _iOwinContext.Get<ApplicationUserManager>() ;
}
private set
{
_userManager = value;
}
}
Solution 8 - C#
(Please note that this answer is for ASP.NET Web API which corresponds to the tag used on the question. See this question if your inquiry is with respect to ASP.NET MVC.)
The question does not specify how the ASP.NET Web API service is to be hosted. The dialog in this post indicates (emphasis mine):
> kichalla wrote Oct 24, 2014 at 1:35 AM > > If you are NOT self-hosting, do not use Microsoft.AspNet.WebApi.Owin > package with IIS...this package is only supposed to be used with self > hosting.
Use of the Microsoft.AspNet.WebApi.Owin
package is recommended in the accepted answer. In this answer I am reporting what has worked for me when hosting an ASP.NET Web API service in IIS.
First, install the following NuGet package:
> Microsoft.Owin.Host.SystemWeb > > OWIN server that enables OWIN-based applications to run on IIS using > the ASP.NET request pipeline.
(Note that as I write, the latest available version of this NuGet package is 3.1.0. Also, to the extent that it might matter, I am using Visual Studio 2013 Update 5.)
After installing this NuGet package, you can do the following:
using Microsoft.Owin;
using System.Web;
IOwinContext context = HttpContext.Current.GetOwinContext();
// or
IOwinContext context = HttpContext.Current.Request.GetOwinContext();
Now, to shed some light on how these statements are resolved. In Visual Studio, if you right-click GetOwinContext
in either statement and select "Peek Definition," Visual Studio will display the following:
// Assembly Microsoft.Owin.Host.SystemWeb.dll, v3.1.0.0
using Microsoft.Owin;
using System;
using System.Runtime.CompilerServices;
namespace System.Web
{
public static class HttpContextExtensions
{
public static IOwinContext GetOwinContext(this HttpContext context);
public static IOwinContext GetOwinContext(this HttpRequest request);
}
}
As you can see, within the System.Web
namespace, there are two GetOwinContext
extension methods:
- One on the
HttpContext
class, and - The other on the
HttpRequest
class.
Again, for those hosting their Web API service in IIS, my hope is that this clears up any ambiguity regarding where a definition for GetOwinContext
can be found with respect to this late date in 2017.
Solution 9 - C#
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 await
ed function.
Solution 10 - C#
After looking at the ASP.NET default project I discovered I needed to include this in my application startup:
// 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);`
Solution 11 - C#
I was missing package: Microsoft.AspNet.WebApi.Owin for ApiControllers. Hope this helps!
Solution 12 - C#
I had this same problem and solved it by using a private method:
private IAuthenticationManager AuthenticationManager { get { return HttpContext.Current.GetOwinContext().Authentication; } }
This worked swimmingly for me.
Solution 13 - C#
I had to add package Microsoft.AspNet.Identity.Owin