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
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.
DONT FORGET to implement the IUserPasswordStore<MyIdentityUser>
interface in your UserStore.cs
class.
For example:
public class UserStore :
IUserStore<MyIdentityUser>,
IUserPasswordStore<MyIdentityUser>,
IUserRoleStore<MyIdentityUser>
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With