public async Task<IActionResult> 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(nameof(VerifyCode), new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe }); }
public async Task<IActionResult> 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 token = await _userManager.GenerateTwoFactorTokenAsync(user, model.SelectedProvider); if (string.IsNullOrWhiteSpace(token)) { return View("Error"); } if (model.SelectedProvider == "Email") { await _mediator.SendAsync(new SendSecurityCodeEmail { Email = await _userManager.GetEmailAsync(user), Token = token }); } else if (model.SelectedProvider == "Phone") { await _mediator.SendAsync(new SendSecurityCodeSms { PhoneNumber = await _userManager.GetPhoneNumberAsync(user), Token = token }); } return RedirectToAction(nameof(VerifyCode), new { Provider = model.SelectedProvider, model.ReturnUrl, model.RememberMe }); }
public async Task SendCodePostReturnsRedirectToActionResult() { var model = new SendCodeViewModel { SelectedProvider = string.Empty, ReturnUrl = "ReturnUrl", RememberMe = true }; var routeValues = new Dictionary<string, object> { ["Provider"] = model.SelectedProvider, ["ReturnUrl"] = model.ReturnUrl, ["RememberMe"] = model.RememberMe }; var userManager = CreateUserManagerMock(); var signInManager = CreateSignInManagerMock(userManager); signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(new ApplicationUser()); userManager.Setup(x => x.GenerateTwoFactorTokenAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>())).ReturnsAsync("token"); var sut = new AdminController(userManager.Object, signInManager.Object, null, null, null); var result = await sut.SendCode(model) as RedirectToActionResult; Assert.Equal(result.ActionName, nameof(AdminController.VerifyCode)); Assert.Equal(result.RouteValues, routeValues); }
public async Task SendCodePostSendsSendSecurityCodeSmsWithCorrectDataWhenSelectedProviderIsPhone() { const string token = "token"; const string usersPhoneNumber = "usersPhoneNumber"; var applicationUser = new ApplicationUser(); var model = new SendCodeViewModel { SelectedProvider = "Phone" }; var userManager = CreateUserManagerMock(); var signInManager = CreateSignInManagerMock(userManager); var mediator = new Mock<IMediator>(); userManager.Setup(x => x.GenerateTwoFactorTokenAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>())).ReturnsAsync(token); userManager.Setup(x => x.GetPhoneNumberAsync(applicationUser)).ReturnsAsync(usersPhoneNumber); signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(applicationUser); var sut = new AdminController(userManager.Object, signInManager.Object, mediator.Object, null, null); await sut.SendCode(model); mediator.Verify(x => x.SendAsync(It.Is<SendSecurityCodeSms>(y => y.PhoneNumber == usersPhoneNumber && y.Token == token))); }
public async Task SendCodePostInvokesGenerateTwoFactorTokenAsyncWithCorrectUserAndTokenProvider() { var applicationUser = new ApplicationUser(); var model = new SendCodeViewModel { SelectedProvider = "Email" }; var userManager = CreateUserManagerMock(); var signInManager = CreateSignInManagerMock(userManager); signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(applicationUser); var sut = new AdminController(userManager.Object, signInManager.Object, null, null, null); await sut.SendCode(model); userManager.Verify(x => x.GenerateTwoFactorTokenAsync(applicationUser, model.SelectedProvider), Times.Once); }
public async Task SendCodePostInvokesSendSmsAsyncWithCorrectParametersWhenSelectedProviderIsPhone() { const string token = "token"; const string usersPhoneNumber = "usersPhoneNumber"; var message = $"Your security code is: {token}"; var applicationUser = new ApplicationUser(); var model = new SendCodeViewModel { SelectedProvider = "Phone" }; var userManager = CreateUserManagerMock(); var signInManager = CreateSignInManagerMock(userManager); var smsSender = new Mock<ISmsSender>(); userManager.Setup(x => x.GenerateTwoFactorTokenAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>())).ReturnsAsync(token); userManager.Setup(x => x.GetPhoneNumberAsync(applicationUser)).ReturnsAsync(usersPhoneNumber); signInManager.Setup(x => x.GetTwoFactorAuthenticationUserAsync()).ReturnsAsync(applicationUser); var sut = new AdminController(userManager.Object, signInManager.Object, null, smsSender.Object, null, null); await sut.SendCode(model); smsSender.Verify(x => x.SendSmsAsync(usersPhoneNumber, message)); }