Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I use ApplicationUserManager in ASP.net?

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?

like image 312
H. Pauwelyn Avatar asked Dec 28 '25 16:12

H. Pauwelyn


1 Answers

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;
        }
    }
like image 60
nest Avatar answered Dec 30 '25 04:12

nest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!