[英]UserManager.FindAsync not working with custom implementation of UserStore
I am relatively new in ASP.NET Identity. 我在ASP.NET身份方面相对较新。 To understand the things better I am doing a custom implementation of ASP.NET Identity. 为了更好地理解这些事情,我正在进行ASP.NET身份的自定义实现。 I am able to create user successfully using the custom code. 我能够使用自定义代码成功创建用户。 However the FindAsync(username, password)
functionality is not working. 但是, FindAsync(username, password)
功能无效。
Here is what I have done so far: 这是我到目前为止所做的:
User : This is my User
class that inherits from IUser<int>
用户 :这是继承自IUser<int>
User
类
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
UserStore :这是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: 在我的MVC5控制器中,我有以下行尝试通过用户名和密码查找用户:
var user = await UserManager.FindAsync("User1355436", "passw0rd");
The above line of code in turn sequentially invokes the following methods of UserStore
: 上面的代码行依次调用UserStore
的以下方法:
public Task<User> FindByNameAsync(string userName)
and public Task<string> GetPasswordHashAsync(User user)
. public Task<User> FindByNameAsync(string userName)
和public Task<string> GetPasswordHashAsync(User user)
。
And after that the code goes to waiting state perpetually and nothing happens, ie 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()
... 要返回包含在任务中的值,请使用Task.FromResult()
...
return Task.FromResult(user.PasswordHash);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.