How could I use ApplicationUserManager in ASP.net?

asp.net c# entity-framework unity-container user-management

Question

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?

1
2
2/15/2016 7:32:23 PM

Accepted Answer

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;
        }
    }
2
2/15/2016 5:59:22 PM

Popular Answer

I've found the error on this question suggested by Steve Greene. I was just missing a using statement.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow