尝试将身份验证添加到ASP.NET MVC Boilerplate模板-异步/等待问题 [英] Trying to add Authentication to ASP.NET MVC Boilerplate template - async / await issue

查看:76
本文介绍了尝试将身份验证添加到ASP.NET MVC Boilerplate模板-异步/等待问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

期待将身份验证添加到 MVC 5 Boilerplate模板,下一段代码在其自己的原始示例中运行良好项目,但是当将其内容集成到样板模板中并尝试注册新用户时,发生冲突并出现浏览器异常,指向以下等待"行:

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,

推荐答案

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

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

       ConfigureAuth(app);

因此启动类如下所示:

    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.

这篇关于尝试将身份验证添加到ASP.NET MVC Boilerplate模板-异步/等待问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆