112

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?

Scottie
  • 11,050
  • 19
  • 68
  • 109
  • It's in the `System.Web.Http` namespace, but it is from the `System.Web.Http.Owin` dll. Have you referenced that? – Simon C Mar 23 '14 at 23:44
  • Ok, I found the nuget package for Microsoft.AspNet.WebApi.Owin and installed that, and included the usings for System.Web.Http and System.Web.Http.Owin, but it still isn't working. – Scottie Mar 23 '14 at 23:53
  • 2
    Sorry, my above comment should have read `System.Net.Http` namespace. So you don't need a using for `.Owin`, just `using System.Net.Http` which you already have. – Simon C Mar 23 '14 at 23:58
  • possible duplicate of [ASP.Net Identity - HttpContext has no extension method for GetOwinContext](http://stackoverflow.com/questions/21148209/asp-net-identity-httpcontext-has-no-extension-method-for-getowincontext) – Darren Wainwright Feb 02 '15 at 13:53

13 Answers13

171

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.

Simon C
  • 9,458
  • 3
  • 36
  • 55
  • The second link worked for me. Just run Install-Package Microsoft.AspNet.WebApi.Owin in your package manager. – Rogala Aug 11 '14 at 21:39
  • Token revocation is too hard!!! But this answer finally helped me get it working (plus manually deleting it from the db, which I have access to). Thank you!! – SlimsGhost May 28 '15 at 02:08
  • I have this problem too but I've used Install-Package Microsoft.Owin.Host.SystemWeb to solve issue – hmfarimani Apr 09 '17 at 23:23
  • 1
    I don't get why this is deemed to be correct - this extension method has nothing to do with WebAPI, and indeed it's in `Microsoft.Owin.Host.SystemWeb`, not `Microsoft.AspNet.WebApi.Owin`. – John Jul 31 '17 at 08:01
  • 3
    @John - If you are using an API controller you need `Microsoft.AspNet.WebApi.Owin`. If you are in a regular MVC controller you will need `Microsoft.Owin.Host.SystemWeb`. The `Request` object is different depending on which Controller type you are using so a different extension method. – Simon C Jul 31 '17 at 22:29
  • Thanks. I should have read the post until its end before to install the first suggested package :). – AFract Nov 14 '17 at 14:08
51

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!)

Obi
  • 3,091
  • 4
  • 34
  • 56
47

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

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

it works in Identity 2.0

ReNiSh AR
  • 2,782
  • 2
  • 30
  • 42
user3572087
  • 506
  • 3
  • 4
23

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

HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
8

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>();
Muhammad Ramzan
  • 342
  • 3
  • 16
5

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

hmfarimani
  • 531
  • 1
  • 8
  • 13
3

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

DavidRR
  • 18,291
  • 25
  • 109
  • 191
2

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;
        }
    }
bigmike3d
  • 37
  • 3
1

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.

AxD
  • 2,714
  • 3
  • 31
  • 53
1

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);`
drobison
  • 858
  • 1
  • 14
  • 23
1

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

D-Go
  • 413
  • 5
  • 9
1

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.

0

I had to add package Microsoft.AspNet.Identity.Owin

GraehamF
  • 1,971
  • 24
  • 24