简体   繁体   English

C#:UserManager.FindAsync总是找到一个匹配项

[英]C#: UserManager.FindAsync always finds a match

UserManager.FindAsync always finds a match even when I input a wrong username and password it always returns with some sort of userID. 即使我输入了错误的用户名和密码, UserManager.FindAsync始终会找到匹配项,它总是以某种用户ID返回。

[HttpPost]
public ActionResult Mobilelogin(LoginViewModel model)
{
    var user = UserManager.FindAsync(model.Email, model.Password);
    if (user != null)
    {
        return new HttpStatusCodeResult(201); // user found
    }
    else
    {
        return new HttpStatusCodeResult(401); // user not found
    }
}

FindAsync returns a Task<TUser> . FindAsync返回Task<TUser> If you work with it synchronously (as you are doing right now), you will always get the Task itself which is not null. 如果您同步处理它(如您现在所做的那样),则您将始终获得Task本身,该Task不为null。

You need to wait on the task asynchronously to get the result (the TUser ) like this: 您需要异步等待任务以获取结果( TUser ),如下所示:

[HttpPost]
public async Task<ActionResult> Mobilelogin(LoginViewModel model)
{
    var user = await UserManager.FindAsync(model.Email, model.Password);
    if (user != null)
    {
        return new HttpStatusCodeResult(201); // user found
    }
    else
    {
        return new HttpStatusCodeResult(401); // user not found
    }
}

Take a look at this reference for more information about asynchronous programming with ASP.NET MVC. 参阅此参考资料 ,以获取有关使用ASP.NET MVC进行异步编程的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM