MVC5默认启用双因素身份验证

时间:2016-04-10 11:46:53

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

我有一个asp.net MVC5应用程序,我有用户注册到系统。我一直在关注我已经运行的http://www.asp.net/mvc/overview/security/aspnet-mvc-5-app-with-sms-and-email-two-factor-authentication教程。我希望在用户注册时默认启用双因素身份验证。

由此我修改了寄存器模型以包含我测试过的Phone,它插入到ASP.Net用户表中没有问题。我的问题是我似乎无法确定如何默认启用两个因素

我是否需要在注册方法中使用类似await UserManager.SupportsUserTwoFactor();的内容?

我的控制器

// 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)
        {
            //  Comment the following line to prevent log in until the user is confirmed.
           // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            var callbackUrl = Url.Action("ConfirmEmail", "Account",
               new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            await UserManager.SendEmailAsync(user.Id,
               "Confirm your account", "Please confirm your account by clicking <a href=\""
               + callbackUrl + "\">here</a>");

            //Creates a Message that will then be displayed onto the 'info' view 
            ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                            + "before you can log in.";

            // var currentUser = UserManager.FindByEmail(user.Email);
            // var roleAssign = UserManager.AddToRole(currentUser.Id, "User");

            //This assigns the user to the default role. Currently it is set to the supplier
            await UserManager.AddToRoleAsync(user.Id, "Supplier");
            //Redirect the user to the following view
            return View("Info");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

可以这样做吗?

0 个答案:

没有答案