Beispiel #1
0
        public async Task <ActionResult> SendCode(SendCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            var user = await signInManager.GetTwoFactorAuthenticationUserAsync();

            if (user == null)
            {
                return(View("Error"));
            }

            // Generate the token and send it
            var code = await userManager.GenerateTwoFactorTokenAsync(user, model.SelectedProvider);

            if (string.IsNullOrWhiteSpace(code))
            {
                return(View("Error"));
            }

            var message = "Your security code is: " + code;

            if (model.SelectedProvider == "Email")
            {
                await emailSender.SendEmailAsync(await userManager.GetEmailAsync(user), "Security Code", message);
            }
            else if (model.SelectedProvider == "Phone")
            {
                await smsSender.SendSmsAsync(await userManager.GetPhoneNumberAsync(user), message);
            }

            return(RedirectToAction("VerifyCode", new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe }));
        }
Beispiel #2
0
        public async Task <TwoFactorInfo> GetTwoFactorInfo(string provider = null)
        {
            IUserContext   userContext = null;
            IList <string> userFactors = new List <string>();
            string         token       = null;
            var            user        = await signInManager.GetTwoFactorAuthenticationUserAsync();

            if (user != null)
            {
                if (!string.IsNullOrWhiteSpace(provider))
                {
                    token = await userManager.GenerateTwoFactorTokenAsync(user, provider);
                }
                userContext = new UserContext(user);
                userFactors = await userManager.GetValidTwoFactorProvidersAsync(user);
            }

            return(new TwoFactorInfo(userContext, userFactors, token));
        }