Problem
I downloaded and run the ASP.NET Identity sample from https://github.com/rustd/AspnetIdentitySample successfully.
I’m currently working on integrating the ASP.NET Identity framework into my project and have run into a bug that has been driving me insane all day…
In the class library, I’m implementing the identification framework. Apart than this, I have installed the newest (pre-release version) of the Identity framework and everything is working properly.
I tried putting the same code in my controller as the same direct and got the same error.
I’m sure I’m missing a reference somewhere, but I’m not sure where…!
The code-block that is causing me the most grief is:
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
I’ve added references to the following – I’ve tested them both in my class library and on the controller directly, and 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 double-checked the versions of Identity and OWIN in the sample and ensured that my solution had the same versions.
Furthermore, I can find GetOwinContext in the object browser on the sample, but it is nowhere to be found in my solution… I’m sure I have an out-of-date library, but I can’t find it!
Asked by Darren Wainwright
Solution #1
ARGH!
I discovered it… I didn’t have the Microsoft.Owin.Host.SystemWeb package installed.
It worked after I found it and installed it.
Now, I’m not sure if I missed something, but I couldn’t find any mention to such a library or package in any of the tutorials. When I installed all of this Identity framework, it didn’t get installed either… I’m not sure if it was only me.
EDIT: Even though it’s in the Microsoft.Owin.Host.SystemWeb assembly, it’s an extension method in the System.Web namespace, so you’ll need to reference the former while using the latter.
Answered by Darren Wainwright
Solution #2
If you’re outside of the controller, I suppose you’ll need to refer to the current HttpContext. The current context is referenced by the MVC controllers. However, you must clearly specify that you want the current HttpContext outside of that.
return HttpContext.Current.GetOwinContext().Authentication;
In terms of why it isn’t showing up, a new MVC 5 project template using the code you provided (the IAuthenticationManager) has the following using statements at the start 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;
After commenting out each one, it looks that GetOwinContext() is a System function. Assemble Web.Mvc.
Answered by Tommy
Solution #3
After comparing the using statements of my controller with the Asp.Net Template controller through try and error
using System.Web;
I was able to solve my problem. You’ll also need to include the following:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
Use the GetUserManager function to get a list of users.
Why hasn’t Microsoft found a way to automatically resolve this with a right-click and resolve like other missing using statements?
Answered by Ronen Festinger
Solution #4
In my situation, I’m going to include Microsoft. AspNet. WebApi. The Owin reference via nuget worked perfectly.
Answered by DolceVita
Solution #5
Make sure the Microsoft.AspNet.Identity.Owin nuget package is installed. Then add the namespace System.Net.Http.
Answered by CodeNotFound
Post is based on https://stackoverflow.com/questions/21148209/asp-net-identity-httpcontext-has-no-extension-method-for-getowincontext