I am relatively new in ASP.NET Identity. To understand the things better I am doing a custom implementation of ASP.NET Identity. I am able to create user successfully using the custom code. However the FindAsync(username, password)
functionality is not working.
Here is what I have done so far:
User: This is my User
class that inherits from IUser<int>
public class User:IUser<int>
{
public User(){ Id = 0; }
public int Id { get; private set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
}
UserStore: This is custom implementation of IUserStore' and 'IUserPasswordStore
public class UserStore : IUserStore<User, int>, IUserPasswordStore<User, int>
{
private IdentityContext _context;
public UserStore()
{
_context = new IdentityContext();
}
public void Dispose()
{
throw new NotImplementedException();
}
#region IUserStore<T,K> members
public Task CreateAsync(User user)
{
_context.Users.Add(user);
_context.SaveChangesAsync();
return Task.FromResult(true);
}
public Task UpdateAsync(User user)
{
throw new NotImplementedException();
}
public Task DeleteAsync(User user)
{
throw new NotImplementedException();
}
public Task<User> FindByIdAsync(int userId)
{
return _context.Users.FirstOrDefaultAsync(user=>user.Id==userId);
}
public Task<User> FindByNameAsync(string userName)
{
var user = _context.Users.SingleOrDefaultAsync(u => u.UserName.Equals(userName));
return user;
}
#endregion
#region IUserPasswordStore<User,int>
public Task SetPasswordHashAsync(User user, string passwordHash)
{
user.PasswordHash = passwordHash;
return Task.FromResult(true);
}
public Task<string> GetPasswordHashAsync(User user)
{
return new Task<string>(() => user.PasswordHash);
}
public Task<bool> HasPasswordAsync(User user)
{
throw new NotImplementedException();
}
#endregion
}
In my MVC5 controller I have the following line that tries find the user by username and password:
var user = await UserManager.FindAsync("User1355436", "passw0rd");
The above line of code in turn sequentially invokes the following methods of UserStore
:
public Task<User> FindByNameAsync(string userName)
and
public Task<string> GetPasswordHashAsync(User user)
.
And after that the code goes to waiting state perpetually and nothing happens, i.e. the control is never returned to the controller again.
What I am missing here?
The problem is likely with this line of code...
return new Task<string>(() => user.PasswordHash);
... this creates a task that is never started, so the code waiting for its continuation waits forever. For returning a value wrapped in a task, use Task.FromResult()
...
return Task.FromResult(user.PasswordHash);