Exemplo n.º 1
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                if (model.Code.IsNullOrWhiteSpace())
                {
                    model.ConfirmationNeeded = true;
                    return(View(model));
                }
                else
                {
                    var confirmationResult = await UserManager.ConfirmEmailAsync(model.Email, model.Code);

                    if (confirmationResult.Succeeded)
                    {
                        result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout : false);

                        if (result != SignInStatus.Success)
                        {
                            model.ConfirmationNeeded = false;
                            ModelState.AddModelError("", "Email confirmed, but login failed. Please try again.");
                            return(View(model));
                        }
                        return(RedirectToLocal(returnUrl));
                    }
                    ModelState.AddModelError("", "Verification failed. Please try again.");
                    model.ConfirmationNeeded = true;
                    return(View(model));
                }
                break;

            // TWO FACTOR??? return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
        public async void Test_GivenAUser_WhenConfirmEmailOrPhoneNumber_ThenResponseIsNotAltered()
        {
            var cognitoUser = GetCognitoUser();

            userStoreMock.Setup(mock => mock.VerifyUserAttributeAsync(It.IsAny <CognitoUser>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
            var output = await userManager.ConfirmEmailAsync(cognitoUser, "code").ConfigureAwait(false);

            Assert.Equal(IdentityResult.Success, output);
            output = await userManager.ConfirmPhoneNumberAsync(cognitoUser, "code").ConfigureAwait(false);

            Assert.Equal(IdentityResult.Success, output);
            userStoreMock.Verify();
        }