1

由于某种原因,EmailCode 未出现在有效的两因素身份验证提供程序中。但是,PhoneCode 一直到我删除它为止,现在什么都没有出现。我已经调试过,它显示在 UserManager 下,但由于某些奇怪的原因 GetValidTwoFactorProvidersAsync 没有检索到它。我已经尝试通过绕过该方法并手动检索值来手动添加它,但随后它会抛出 Microsoft.AspNet.Identity.EmailTokenProvider 不存在的错误消息。我不知道为什么这不起作用。

        public async Task<ActionResult> SendCode(string returnUrl)
        {
            var userId = await SignInManager.GetVerifiedUserIdAsync();
            if (userId == null)
            {
                return View("Error");
            }
            var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(userId);
            var factorOptions = userFactors.Select(purpose => new SelectListItem { Text = purpose, Value = purpose }).ToList();
            return View(new SendCodeViewModel { Providers = factorOptions, ReturnUrl = returnUrl });
        }

身份配置

            manager.RegisterTwoFactorProvider("EmailCode", new Microsoft.AspNet.Identity.EmailTokenProvider<SystemUser>
                {
                    Subject = "SecurityCode",
                    BodyFormat = "Your security code is {0}"
                });

            manager.EmailService = new EmailService();
            var dataProtectionProvider = options.DataProtectionProvider;

            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider =
                    new DataProtectorTokenProvider<SystemUser>(
                        dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
4

2 回答 2

4

确保您有用户的 ConfirmedEmail。您可以在数据库中查找用户并查看 EmailConfirmed 标志以查看它是否已设置。

于 2015-01-16T22:36:58.543 回答
0

由于我看不到您的代码,因此无法提出适当的修复建议,但我可以告诉您我是如何做到的,一切顺利。当我尝试使用这些链接为 2 路身份验证配置应用程序时,它对我来说非常方便和容易:

http://www.asp.net/identity/overview/features-api/two-factor-authentication-using-sms-and-email-with-aspnet-identity

http://typecastexception.com/post/2014/04/20/ASPNET-MVC-and-Identity-20-Understanding-the-Basics.aspx

如果您还没有,建议您尝试一下。希望能帮助到你!

于 2014-12-08T08:31:09.857 回答