Search code examples
c#asp.net-mvcasp.net-identity-2

Build a custom user check password in Asp.Net Identity 2


I need to build a custom user password check in an application implemented in asp.net MVC 5 and using Asp.Net Identity 2.

I read in a stackoverflow post (Writing a custom IUserPasswordStore and SignInManager.PasswordSignInAsync in Identity 2.1) that I only need to override the CheckPasswordAsync method in UserManager.

I try to override this method in IdentityConfig.cs file. Here is the code that I add to the ApplicationUserManager class just for test this solution:

public override async Task<bool> CheckPasswordAsync(ApplicationUser user,   string password)
{
        return await Task.Run(() => {
            return true;
        });
}

The problem is that this code is never run in the login process, and the login always fail. To sign in the user I’m using the SignInManager.PasswordSignInAsync to log in the user, this is the default when creating a new web application in asp.net MVC 5. Shouldn’t this method call the ApplicationUserManager. CheckPasswordAsync? Or there is another configuration needed to this work?


Solution

  • The problem was that I was trying to login with a user that do not exists in the system.

    The SignInManager.PasswordSignInAsync never invoke the ApplicationUserManager. CheckPasswordAsync if the user not exists in the user store repository.

    In conclusion, I have to store the users in my application or implement a custom user store mechanism.