ASP.NET Identity - HttpContext has no extension method for GetOwinContext

177,074

Solution 1

ARGH!

I found it... I didn't have an extra package, called Microsoft.Owin.Host.SystemWeb

Once i searched and installed this, it worked.

Now - i am not sure if i just missed everything, though found NO reference to such a library or package when going through various tutorials. It also didn't get installed when i installed all this Identity framework... Not sure if it were just me..

EDIT Although it's in the Microsoft.Owin.Host.SystemWeb assembly it is an extension method in the System.Web namespace, so you need to have the reference to the former, and be using the latter.

Solution 2

I believe you need to reference the current HttpContext if you are outside of the controller. The MVC controllers have a base reference to the current context. However, outside of that, you have to explicitly declare you want the current HttpContext

return HttpContext.Current.GetOwinContext().Authentication;

As for it not showing up, a new MVC 5 project template using the code you show above (the IAuthenticationManager) has the following using statements at the top of the account controller:

using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using WebApplication2.Models;

Commenting out each one, it appears the GetOwinContext() is actually a part of the System.Web.Mvc assembly.

Solution 3

After trial and error comparing the using statements of my controller and the Asp.Net Template controller

using System.Web;

Solved the problem for me. You are also going to need to add:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;

To use GetUserManager method.

Microsoft couldn't find a way to resolve this automatically with right click and resolve like other missing using statements?

Solution 4

In my case adding Microsoft.AspNet.WebApi.Owin reference via nuget did the trick.

Solution 5

Make sure you installed the nuget package Microsoft.AspNet.Identity.Owin. Then add System.Net.Http namespace.

Share:
177,074
Darren Wainwright
Author by

Darren Wainwright

Updated on March 14, 2020

Comments

  • Darren Wainwright
    Darren Wainwright about 4 years

    I have downloaded, and successfully ran the ASP.NET Identity sample from here: https://github.com/rustd/AspnetIdentitySample

    I am now in the middle of implementing the ASP.NET Identity framework in my project and have ran into a problem, that has driven me mad all day...

    GetOwinContext() does not exist as an extension method on my HttpContext

    I am implementing the identity framework in class library. I have installed all the latest (pre-release version) of the Identity framework and everything - apart from this - is working fine.

    I have tried implementing the same code as the same direct in my controller, and find the same problem.

    I'm clearly missing a reference somewhere, though I have no idea what..!..

    The code-block that is killing me is:

    private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }
    

    I have added references to the following - tried these both in my class library and also direct on the controller, none of them work for me...

    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using Microsoft.Owin.Security;
    using Microsoft.Owin;
    using System.Web;
    

    ... this is driving me up the wall....any idea?

    UPDATE

    I have checked the versions of Identity & OWIN in the sample, and I have made sure I have the same versions in my solution.

    More so, if I search the object browser on the sample for GetOwinContext I can find the method, however when I search for it in my solution it is nowhere to be found... I must have some library out of date, but I can't find it!

  • Darren Wainwright
    Darren Wainwright over 10 years
    Been trying this and just can't find it. - the GetOwinContext() i mean.
  • Tommy
    Tommy over 10 years
    @Darren - I played with the sample template a little bit and may have the assembly you are missing. Looks like you will need to reference System.Web.Mvc from your class project. When commented out, that is what made HttpContext.GetOwinContext() become unknown (and not the Microsoft.Owin.Security assembly)
  • Darren Wainwright
    Darren Wainwright over 10 years
    I've been trying to get this to work directly on a controller too - and again can't find it.
  • Darren Wainwright
    Darren Wainwright over 10 years
    I had that one in too. Turns out there was another library NOT installed when the Identity stuff was installed.
  • frenchie
    frenchie about 10 years
    Thanks for figuring that one out: I'm using this tutorial asp.net/identity/overview/getting-started/… and it's a missing step
  • Darren Wainwright
    Darren Wainwright about 10 years
    Seems it's in there now - either that or we both missed it before :)
  • tne
    tne about 10 years
    Instead of running around aimlessly, installing random libraries and expecting tutorials to be reference documentation, one should rather lookup the method in the actual reference, then follow the link to its class (on the left). Then, it's right in front of you: Assembly: Microsoft.Owin.Host.SystemWeb (in Microsoft.Owin.Host.SystemWeb.dll). Literally one click.
  • Darren Wainwright
    Darren Wainwright about 10 years
    @tne - thank you for your comment... though I was hardly 'running around aimlessly' - i was following a tutorial from a trusted resource; from the asp.net site no-less. They didn't have that step in their tutorial (it's since been added) - also, the fact people are upvoting both this answer and the original Q also goes to indicate that I wasn't the only one with this issue.
  • tne
    tne about 10 years
    @Darren, fair enough. (I do realize I could have worded that differently.)
  • Chris Bohatka
    Chris Bohatka about 10 years
  • David Miani
    David Miani almost 10 years
    This tripped me up. There is a public property HttpContext on the Controller class, so if you inherit from that, HttpContext.GetOwinContext() works as expected. If you inherit from ApiController though, there is no such property. Therefore, HttpContext will refer to the class, which will require you to use Current to access an instance of the class. So if you copy/paste code from a Controller to an ApiController and wonder why it doesn't work, this is the answer for you.
  • hofnarwillie
    hofnarwillie over 9 years
    You also need the using System.Web; statement
  • SouthShoreAK
    SouthShoreAK about 9 years
    This worked for me as well. I already had the Identity libraries, but it seems to need System.Web.
  • decades
    decades about 9 years
    None of the receipts here did help me so far :(
  • shannon
    shannon about 9 years
    This has apparently been moving around a lot, both in assemblies and in namespaces. Makes it really painful with it being an extension method. I've updated all of my libraries and this answer reflects where it currently is.
  • Phil
    Phil about 9 years
    Thanks for this!! I've probably been following the same tutorials as you and they don't always include everything you need!
  • enorl76
    enorl76 over 8 years
    This is EXACTLY why I hate extension methods, having to have just the right namespace in your using statements
  • JustJohn
    JustJohn over 8 years
    ah, ye ol' dll hell in a new shape
  • Ali Umair
    Ali Umair over 8 years
    using Microsoft.AspNet.Identity.Owin; i missed this one. GOD i have started to hate extension methods.
  • Vinney Kelly
    Vinney Kelly over 8 years
    There seems to be a new wrinkle in the elusive GetOwinContext() extension method. The method, as defined in the Microsoft.Owin.Host.Systemweb assembly, as referenced above, is an extension of the HttpContextBase class. It appears that the new Controller in Microsoft.AspNet.Mvc v6.0.0-rc1 new returns HttpContext instead of the HttpContextBase.
  • Nordes
    Nordes over 8 years
    How did you fix your build to make it works the first time?
  • Nick
    Nick almost 8 years
    Referencing Microsoft.Owin.Host.SystemWeb didn't do it for me. I'm using Owin 3.0.1. What fixed it though was referencing System.Web.Http.Owin in the project references.
  • oatsoda
    oatsoda almost 8 years
    Yes, this did it for me on the current latest version. The extension method is in this Package, which is Assemby System.Web.Http.Owin and within the Namespace System.Net.Http
  • Maverick
    Maverick almost 8 years
    @Nordes Sometimes VS fails to build in the background (ie: it tells you there are compile errors), but triggering a real build (or more often clean and then build) will get it through. That's likely what Kevin was experiencing.
  • Roi Shabtai
    Roi Shabtai over 7 years
    @Darren Please add package name. Thank you
  • Click Ok
    Click Ok over 7 years
    "Current" was the gotcha for me
  • niico
    niico over 7 years
    Makes rolling your own simple authentication seem like a great idea
  • WiSeeker
    WiSeeker about 7 years
    @SaturnTechnologies For me, the other package was Microsoft.Owin.Host.SystemWeb and namespace System.Web
  • TPG
    TPG almost 7 years
    Nice work, just need to find for "Microsoft.Owin.Host.SystemWeb" in nuget and install it. What a shame Microsoft tutorial itself didn't point that out.
  • Ronen Festinger
    Ronen Festinger over 6 years
    Yes, sadly, extension methods can't be resolved automatically.
  • Ortund
    Ortund about 6 years
    Don't know if it was from updating this reference, but I had to change up my assignment a little bit to var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; (Current included where it wasn't there in the question).
  • Alexandre
    Alexandre about 6 years
    This did it for me! Thx
  • Mike
    Mike about 6 years
    @Ortund - yes, I think for web forms this is what you need to do, the one without seems to be for MVC
  • Max Barraclough
    Max Barraclough almost 6 years
    I second WiSeeker. For me, this didn't help. Darren's suggestion to install Microsoft.Owin.Host.SystemWeb did the trick though.
  • Praveen Kumar Rejeti
    Praveen Kumar Rejeti almost 6 years
    Worked for me too
  • Dov Miller
    Dov Miller over 5 years
    I have a Web Site not MVC and adding Current was enough to solve the problem. Thank you.
  • Jeff Mergler
    Jeff Mergler over 5 years
    Installing this Microsoft.Owin.Host.SystemWeb package also solved my problem with my error "getowincontext is not a member of system.web.httpcontext" but I was getting a Nuget errors trying to install it To get it installed, I first changed my target .NET Framework to 4.5.1 or higher** and then Nuget installed Microsoft.Owin.Host.SystemWeb just fine (** My original project targeted .NET Framework 4.5.0 and Microsoft.Owin.Host.SystemWeb would not install for me on 4.5.0.)
  • Rei Miyasaka
    Rei Miyasaka almost 4 years
    Why is this so messy