IsEmailConfirmedAsync有一些无效的参数

时间:2017-02-06 18:37:25

标签: c# asp.net-mvc asp.net-identity

我正在使用MVC 5的脚手架代码生成login方法。我遵循了......的官方教程。

Create a secure ASP.NET MVC 5 web app with log in, email confirmation and password reset (C#)

...添加额外的功能,确保在用户登录系统之前确认电子邮件。

以下是我在控制器中的代码:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var currentUser = UserManager.FindByNameAsync(model.Email);
    if (currentUser != null)
    {
        if (!await UserManager.IsEmailConfirmedAsync(currentUser.Id))
        {
            ViewBag.errorMessage = "You must have a confirmed email to log on.";
            return View("Error");
        }
    }

    // Other scaffolded implementations
}

但是,Visual Studio会出现一个错误,指出该参数对方法IsEmailConfirmedAsync无效。显然,我检查了currentUser.Idint数据类型,并且是id的{​​{1}}。如何解决这个问题,以便我传递的是System.Threading.Task而不是任务UserId

1 个答案:

答案 0 :(得分:1)

这是因为在您的代码currentUser中分配了从找到用户返回的Task

您应该等待该呼叫以获得所需的行为

var currentUser = await UserManager.FindByNameAsync(model.Email);

即使是与OP相关联的示例也是如此

// Require the user to have a confirmed email before they can log on.
var user = await UserManager.FindByNameAsync(model.Email);
if (user != null)
{
   if (!await UserManager.IsEmailConfirmedAsync(user.Id))
   {
      ViewBag.errorMessage = "You must have a confirmed email to log on.";
      return View("Error");
   }
}