Store does not implement IUserPasswordStore<TUser> ASP.NET Identity
Asked Answered
A

3

7

In the template project generated by visual studio for ASP.NET identity authentication for Web Api 2 project, I added a class ApplicationUserStore(thats my custom userstore)

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 wanted FMBP_Company object whenever I do FindByIdAsync.

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

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

Once I did this, I am getting an error while doing registration.Following is the error.

Store does not implement IUserPasswordStore

Addiel answered 5/6, 2015 at 3:38 Comment(3)
So have you then updated ApplicationUserStore to implement the IUserPasswordStore interface? You would then have public class ApplicationUserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>Sanctimonious
@BrendanGreen yes I have implemented this interface now, but I do not know what to implement here as I do not need customized code for password yetAddiel
weblogs.asp.net/imranbaloch/… solved my probelmAddiel
P
11

Answering your question. During the authentication unless you implement something very particular you need to implement more than one interface because once you take control of the manager and repository you have to provide the code to handle several things. Even if you don't want to implement a customized password hashing the interface must be implemented due to the fact the manager has been overriden and there's not more implementation of the password handling.

If you don't want to rewrite anything about the hashing process implement IUserPasswordStore and simply 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 normally of IUser (it could be an inherited class of course)

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

Phooey answered 29/12, 2015 at 12:55 Comment(0)
P
3

DONT FORGET to implement the IUserPasswordStore<MyIdentityUser> interface in your UserStore.cs class.

For example:

public class UserStore : 
  IUserStore<MyIdentityUser>,
  IUserPasswordStore<MyIdentityUser>,
  IUserRoleStore<MyIdentityUser>
{
  ...
}
Peadar answered 27/3, 2019 at 0:6 Comment(0)
O
0

Correct answer is with IUserEmailStore

Public Interface ICustomUserStore(Of T As Class)
    Inherits IUserStore(Of T)
    Inherits IUserPasswordStore(Of T)
    Inherits IUserRoleStore(Of T)
    Inherits IUserEmailStore(Of T)
    Inherits IUserPhoneNumberStore(Of T)
    Inherits IUserAuthenticatorKeyStore(Of T) 'TwoFactor
    Inherits IUserTwoFactorStore(Of T)        'TwoFactor
    Inherits IUserTwoFactorRecoveryCodeStore(Of T)        'TwoFactor
    Inherits IUserLoginStore(Of T) 'download personal data
    'IUserPasswordStore
    Overloads Function SetPasswordHashAsync(user As T, passwordHash As String, cancellationToken As CancellationToken) As Task
    Overloads Function GetPasswordHashAsync(user As T, cancellationToken As CancellationToken) As Task(Of String)
    Overloads Function HasPasswordAsync(user As T, cancellationToken As CancellationToken) As Task(Of Boolean)
    'IUserRoleStore
    Overloads Function AddToRoleAsync(user As T, roleName As String, cancellationToken As CancellationToken) As Task
    Overloads Function RemoveFromRoleAsync(user As T, roleName As String, cancellationToken As CancellationToken) As Task
    Overloads Function GetRolesAsync(user As T, cancellationToken As CancellationToken) As Task(Of IList(Of String))
    Overloads Function IsInRoleAsync(user As T, roleName As String, cancellationToken As CancellationToken) As Task(Of Boolean)
    Overloads Function GetUsersInRoleAsync(roleName As String, cancellationToken As CancellationToken) As Task(Of IList(Of T))
    'IUserEmailStore
    Overloads Function SetEmailAsync(user As T, email As String, cancellationToken As CancellationToken) As Task
    Overloads Function GetEmailAsync(user As T, cancellationToken As CancellationToken) As Task(Of String)
    Overloads Function GetEmailConfirmedAsync(user As T, cancellationToken As CancellationToken) As Task(Of Boolean)
    Overloads Function SetEmailConfirmedAsync(user As T, confirmed As Boolean, cancellationToken As CancellationToken) As Task
    Overloads Function FindByEmailAsync(normalizedEmail As String, cancellationToken As CancellationToken) As Task(Of T)
    Overloads Function GetNormalizedEmailAsync(user As T, cancellationToken As CancellationToken) As Task(Of String)
    Overloads Function SetNormalizedEmailAsync(user As T, normalizedEmail As String, cancellationToken As CancellationToken) As Task
End Interface
Opisthognathous answered 9/8, 2022 at 21:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.