1

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

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

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

  • 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);
    }
4

1 回答 1

1

Create empty web application and use package manager console to install following asp.net identity framework sample from NuGet and play around to understand asp.net identity. This sample included end to end sample application which included normal email registration and two-factor authentication which you exactly asking in your question.

Install-Package Microsoft.AspNet.Identity.Samples

Hope this helps.

于 2014-09-19T13:43:53.320 回答