Esempio n. 1
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Member member = _authenticationService.ValidateUser(model.Username, model.Password);

                if(member != null)
                {
                    //AddNotification(Constants.Account.SuccessfulLoginMessage);

                    var cookie = _authenticationService.GenerateCookieFor(member);
                    Response.Cookies.Add(cookie);

                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public void LoginAction_CookieIsGenerated_OnValidLogin()
        {
            // Setup
            Member member = new Member();
            LoginViewModel viewModel = new LoginViewModel { Username = string.Empty, Password = string.Empty };
            Mock<IAuthenticationService> fakeauthenticationService = new Mock<IAuthenticationService>();

            fakeauthenticationService.Setup(a => a.ValidateUser(It.IsAny<string>(), It.IsAny<string>())).Returns(member);
            AccountController controller = new AccountController(fakeauthenticationService.Object);

            // Action
            //var result = controller.Login(viewModel, string.Empty);

            //// Assert
            //fakeauthenticationService.Verify(a => a.GenerateCookieFor(member), Times.Once());
        }