尝试将身份验证添加到 ASP.NET MVC 样板模板 - 异步/等待问题

Trying to add Authentication to ASP.NET MVC Boilerplate template - async / await issue

期待向 MVC 5 Boilerplate template, The next piece of code worked well in its own original sample project 添加身份验证,但是当将其内容集成到样板模板中并尝试注册新用户时,出现冲突并出现浏览器异常,指向以下 "await"行:

// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
   if (ModelState.IsValid)
   {
      var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
      var result = await UserManager.CreateAsync(user, model.Password);
      if (result.Succeeded)
      {
         string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");
         ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                         + "before you can log in.";
         ViewBag.Link = callbackUrl;
         return View("Info");
      }
      AddErrors(result);
   }
   return View(model);
}

我在很多地方都读到过,当发生异步问题时,人们建议将其设为同步,但就我而言,很多事情会变得 incompatible.I 想知道如何保持此方法的原始异步in the template

您的 GetItems 不需要 await,因此不应该 async。只需将签名更改为:

private List<SyndicationItem> GetItems(CancellationToken cancellationToken)

并将调用代码更改为:

await GetItems(token);

至:

GetItems(token);

感谢 Nikita1315 的帮助,我发现 startup.cs 文件中缺少下一个配置语法:

       ConfigureAuth(app);

所以启动 class 看起来像:

    public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureContainer(app);
        ConfigureAuth(app);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    }
}

所以,只要我添加了这个配置,并且不修改任何异步方法,我就可以注册我的第一个用户。