Store does not implement IUserPasswordStore <TUser> ASP.NET ID

In a template project created by visual studio for ASP.NET Identity Authentication for Web Api 2 project, I added ApplicationUserStore class (this is my custom custom store)

public class ApplicationUserStore : IUserStore<ApplicationUser>
{
    private readonly ApplicationDbContext _context;

    public ApplicationUserStore(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<ApplicationUser> FindByIdAsync(string userName)
    {
        return await _context.Users.Include(x => x.FMBP_Company).FirstOrDefaultAsync(n => n.UserName == userName);
    }

    public Task CreateAsync(ApplicationUser user)
    {
        throw new NotImplementedException();
    }

    public Task DeleteAsync(ApplicationUser user)
    {
        throw new NotImplementedException();
    }

    public Task<ApplicationUser> FindByNameAsync(string userName)
    {
        return _context.Users.Include(x => x.FMBP_Company).FirstOrDefaultAsync(n => n.UserName == userName);
    }

    public Task UpdateAsync(ApplicationUser user)
    {
        throw new NotImplementedException();
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

      

I basically did this because I need an FMBP_Company object whenever I find FindByIdAsync.

I even changed IdentiyConfig.cs and added the following line so that I can use my custom store.

var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));

      

Once I did that, I get an error during registration. Error is error.

The store does not implement IUserPasswordStore

+3


source to share


1 answer


Answering your question. During authentication, unless you are implementing something very specific, you need to implement more than one interface, because once you take over the manager and repository, you have to provide code to handle multiple things. Even if you do not want to implement a customized password hashing, the interface must be implemented due to the fact that the manager was overridden and there was no more implementation of password handling.

If you don't want to rewrite anything about the hashing process, do the IUserPasswordStore and just write:

        public Task SetPasswordHashAsync(YourUser user, string passwordHash)
        {
            user.PasswordHash = passwordHash;
            return Task.FromResult(0);
        }
        public Task<string> GetPasswordHashAsync(YourUser user)
        {
            return Task.FromResult<string>(user.PasswordHash);
        }
        public Task<bool> HasPasswordAsync(YourUser user)
        {
            return Task.FromResult<bool>(!String.IsNullOrEmpty(user.Password));
        }

      



Where YourUser is the current user, usually IUser (this could be an inherited class of course)

Look at the code, it's very simple, just assigning a hashed password to the object, returning what is in the object and a boolean that checks if the user has a password.

+4


source







All Articles