双因素身份验证不包含GetEmailConfirmationTokenAsync的定义

时间:2014-09-19 08:47:32

标签: c# asp.net two-factor-authentication

我正在尝试实施双因素身份验证。在双因素身份验证中,我想生成6位数的令牌,这个6位数的令牌发送到用户邮件ID。

问题是当我在AccountController中实现Register方法时,我在这个方面遇到错误 GetEmailConfirmationTokenAsync

我安装了所有必要的包Like,

  • Microsoft.AspNet.Identity.Owin;

  • Microsoft.AspNet.Identity;

  • Microsoft.AspNet.Identity.EntityFramework;

  • Microsoft.Owin.Security;

  • Microsoft.AspNet.Identity.Sample;

  • Microsoft.AspNet.Identity.Core

错误:双因素身份验证不包含GetEmailConfirmationTokenAsync的定义。

我指的是这两个链接:

http://blogs.msdn.com/b/webdev/archive/2014/02/18/adding-two-factor-authentication-to-an-application-using-asp-net-identity.aspx

http://blogs.msdn.com/b/webdev/archive/2014/02/12/per-request-lifetime-management-for-usermanager-class-in-asp-net-identity.aspx

代码:

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
           //var user = new ApplicationUser() { UserName = model.UserName };

            var user = new ApplicationUser() { UserName = model.UserName, Email = model.Email };

            var result = await UserManager.CreateAsync(user, model.Password);
            //if (result.Succeeded)
            //{
               // await SignInAsync(user, isPersistent: false);
               // return RedirectToAction("Index", "Home");
            //}


            if (result.Succeeded)
            {
                string code = await UserManager.GetEmailConfirmationTokenAsync(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 this link: <a href=\"" + callbackUrl + "\">link</a>");
                return View("ShowEmail");
            }
            else
            {
                AddErrors(result);
            }
        }

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

1 个答案:

答案 0 :(得分:1)

创建空Web应用程序并使用包管理器控制台从NuGet安装以下asp.net标识框架示例,并了解asp.net标识。此示例包括端到端示例应用程序,其中包括您在问题中确切询问的正常电子邮件注册和双因素身份验证。

安装包Microsoft.AspNet.Identity.Samples

希望这有帮助。