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

老子叫甜甜 提交于 2019-12-13 08:33:27

问题


Looking forward to adding authentication to the MVC 5 Boilerplate template, The next piece of code worked well in its own original sample project, but when integrated its content into the Boilerplate template, and tried to register a new user, something become conflicting and a browser exception appears, pointing to the following "await" line:

// 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);
}

I've read in many places that when an async issue happens, people advise to make it synchronous, but for my case many things would become incompatible.I wonder how to keep this method async as it was originaly in the template,


回答1:


Your GetItems doesn't need to await, so it shouldn't be async. Just change the signature to:

private List<SyndicationItem> GetItems(CancellationToken cancellationToken)

and change the calling code from:

await GetItems(token);

to:

GetItems(token);



回答2:


Thanks to Nikita1315 help, I could find that the next configuration syntax was missing in the startup.cs file:

       ConfigureAuth(app);

so the startup class would look like:

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

So, as soon as I added this configuration, and without modifying any async method, I could register my first user.



来源:https://stackoverflow.com/questions/33484905/trying-to-add-authentication-to-asp-net-mvc-boilerplate-template-async-await

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!