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
I had to add package Microsoft.AspNet.Identity.Owin
(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:
HttpContext
class, andHttpRequest
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.
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;
}
}
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
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.
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);`