3

I have created a controller named ProfileContoller in my application. In this controller I need to creat an instance of ApplicationUserManager however I don't know how to create a new instance of it. I've tried code below:

private ApplicationUserManager _userManager = Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

but I got this error:

The name Current does not exist in the current context

My first question is how could I do this? I'll acces this line of code:

return View(await _userManager.FindByNameAsync(username));

I also use an UnityContainer. My second question about this is: could I also register the ApplicationUserManager associated with the interface IUser or IUserStore1.


Update:

Later I've found there is something like this:

private UserManager<ApplicationUser> _userManager = UserManager<ApplicationUserManager>();

Bit it give me this error:

Non-invocable member UserManager<TUser> cannot be used like a method.

With this extra "user manager", I can't see the I can't see the wood for the trees. So once more can you explain me what all the "user managers" mean and what they are used for?


Notes:
1 I don't know the difference between this two so if you'll, can you explane this too?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • 1
    Do you have the correct using statements? http://stackoverflow.com/questions/24001245/cant-get-usermanager-from-owincontext-in-apicontroller – Steve Greene Feb 15 '16 at 18:44
  • um regarding the `cannot be used like a method` -- are you missing the `new` keyword? -- It really does look like you're trying to invoke a type like it's a method... – BrainSlugs83 Apr 06 '17 at 21:32

2 Answers2

2

This works for me:

using System.Web;  //make sure you have this using

    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            if (_userManager == null && HttpContext == null)
            {
                return new ApplicationUserManager(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(db));
            }
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
nest
  • 1,385
  • 1
  • 15
  • 34
  • 5
    Where does the type `ApplicationUserManager` come from? -- It doesn't exist by default; do you have a class named this somewhere? -- Also your `_userManager` cache value never gets set in this case, perhaps instead of just returning the value you should set it first, and then return it as a second step. – BrainSlugs83 Apr 06 '17 at 21:34
0

I have not looked at this in VS so am not 100% however the error message you are getting

The name Current does not exist in the current context

could just be down to the fact that current is part of the http context

try

public class ProfileController : Controller
{

    private ApplicationUserManager _userManager;

    public ProfileController()
    {
        _userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }

So the change here is that _userManager is being declared but not set. We are then setting it in the constructor for the class.

D3vy
  • 831
  • 7
  • 19
  • No I'm sorry. Now I've got this error **A field initializer cannot reference the non-static field, method, or property `Controller.HttpContext`** – H. Pauwelyn Feb 15 '16 at 17:44
  • I have edited with an update that I think will help. – D3vy Feb 15 '16 at 17:51
  • I doesn't work also. I've this error **'IOwinContext' does not contain a definition for 'GetUserManager' and no extension method 'GetUserManager' accepting a first argument of type 'IOwinContext' could be found (are you missing a using directive or an assembly reference?)** on `GetUserManager()`. – H. Pauwelyn Feb 15 '16 at 17:56
  • In that case you are missing a reference somewhere - GetOwinContext is an extension method in the Microsoft.Owin namespace. Make sure that you have Microsoft.Owin, Microsoft.Owin.Host.SystemWeb and Microsoft.Owin.Security referenced. – D3vy Feb 15 '16 at 18:01