Create a user password for password verification in Asp.Net Identity 2

I need to create a custom user password check in an application implemented in asp.net MVC 5 and use 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 am trying to override this method in the IdentityConfig.cs file. Here is the code I'm adding to the ApplicationUserManager class to 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 never runs during the login process, and the login always fails. To log in to the Im user account using SignInManager.PasswordSignInAsync to log in, this is the default value when creating a new web application in asp.net MVC 5. Should this method call ApplicationUserManager. CheckPasswordAsync? Or is there another configuration needed for this to work?

+4
source share
3 answers

The problem was that I was trying to log in with a user who is not on the system.

SignInManager.PasswordSignInAsync never calls ApplicationUserManager. CheckPasswordAsync if the user does not exist in the user's data store repository.

.

+3

. ASP.NET MVC, , NuGet, .

, - , .

ApplicationUserManager :

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

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

private bool MyCheckPasswordAsync()
{
    return true;
}

, :

enter image description here

+7

This may not be a direct answer, but it provides a complete solution to the problem. It implements a custom authorization filter, which you can then configure to accomplish what you want.

https://weblog.west-wind.com/posts/2013/Apr/18/A-WebAPI-Basic-Authentication-Authorization-Filter

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class BasicAuthenticationFilter : AuthorizationFilterAttribute

It can then be used like this instead of the [Login] attribute

[MyBasicAuthenticationFilter]
public class QueueController : ApiController
0
source

Source: https://habr.com/ru/post/1599049/


All Articles