Search code examples
asp.net-identityusermanager

UserManager.FindAsync to use custom Userstore's FindByIdAsync method instead of FindByNameAsync


I'm using Identity Framework. When I login to my app, the appUser retrieves the UserManager as follow:

Patient patient = await UserManager.FindAsync(model.Id, model.Password);

As I understand from debugging, UserManager.FindAsync uses the FindByNameAsync(string userName) from the UserStore (I have a custom UserStore).

The question is: How to tell UserManager.FindAsync to use the FindByIdAsync method of my custom UserStore instead of FindByNameAsync?

Do I need to also write a custom UserManager?

Thanks in advance.


Solution

  • You can override the method in a custom UserManager

    public class ApplicationUserManager : UserManager<AppUser, string>
    {
         public override async Task<AppUser> FindAsync(string id, string password)
         {
             var user = await FindByIdAsync(id).WithCurrentCulture();
             if (user == null)
             {
                 return null;
             }
             return await CheckPasswordAsync(user, password).WithCurrentCulture() ? user : null;
         }   
    }