public async Task<ActionResult> Login(LoginModel model)
        {
            if (!model.IsValid())
            {
                SetPleaseTryAgain(model);
                return View(model);
            }

            var isAuthenticated = await _userService.Authenticate(model.Email, model.Password);
            if (!isAuthenticated)
            {
                SetPleaseTryAgain(model);
                return View(model);
            }

            var user = await _userService.GetByEmail(model.Email);

            _formsAuthenticationService.SignIn(user.Id, user.FullName, user.Email, user.RoleId, true);

            if (!string.IsNullOrEmpty(model.ReturnUrl))
            {
                return Redirect(model.ReturnUrl);
            }

            return RedirectToHome();
        }
 public ViewResult Login()
 {
     var model = new LoginModel();
     return View(model);
 }
        public async void login_should_redirect_if_model_is_valid()
        {
            // Arrange
            const string actionName = "Login";
            const int id = 1;
            const string email = "*****@*****.**";
            const string fullName = "test";
            const string password = "******";
            var validModel = new LoginModel { Password = password, Email = email };

            var userService = new Mock<IUserService>();
            userService.Setup(x => x.Authenticate(email, password))
                    .Returns(() => Task.FromResult(true));
            userService.Setup(x => x.GetByEmail(email))
                    .Returns(() => Task.FromResult(new UserDto { Id = id, FullName = fullName, Email = email, RoleId = SetRole.User.Value }));

            var formsAuthenticationService = new Mock<IFormsAuthenticationService>();
            formsAuthenticationService.Setup(x => x.SignIn(id, fullName, email, SetRole.User.Value, true));

            // Act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                                                 .WithFormsAuthenticationService(formsAuthenticationService.Object)
                                                 .Build();
            var view = await sut.Login(validModel) as RedirectResult;

            // Assert
            Assert.NotNull(view);
            Assert.AreEqual(view.Url, "/");
            Assert.IsInstanceOf<BaseController>(sut);

            userService.Verify(x => x.Authenticate(email, password), Times.Once);
            formsAuthenticationService.Verify(x => x.SignIn(id, fullName, email, SetRole.User.Value, true), Times.Once);

            sut.AssertPostAttribute(actionName, new[] { typeof(LoginModel) });
            sut.AssertAllowAnonymousAttribute(actionName, new[] { typeof(LoginModel) });
        }