public ActionResult Login(LogOnFormModel form, string returnUrl) { if (ModelState.IsValid) { User user = userRepository.Get(u => u.Email == form.UserName && u.Activated == true); if (user != null) { if (ValidatePassword(user, form.Password)) { formAuthentication.SetAuthCookie(this.HttpContext, UserAuthenticationTicketBuilder.CreateAuthenticationTicket( user)); 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 return(Json(new { errors = GetErrorsFromModelState() })); }
public ActionResult Login(LogOnFormModel form, string returnUrl) { if (ModelState.IsValid) { User user = userRepository.Get(u => u.Email == form.UserName && u.Activated == true); if (user != null) { if (ValidatePassword(user, form.Password)) { formAuthentication.SetAuthCookie(this.HttpContext, UserAuthenticationTicketBuilder.CreateAuthenticationTicket( user)); 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 return Json(new { errors = GetErrorsFromModelState() }); }
public ActionResult LogOn(LogOnFormModel form, string returnUrl) { if (ModelState.IsValid) { User user = userRepository.Get(u => u.Email == form.UserName && u.Activated == true); if (user != null) { if (ValidatePassword(user, form.Password)) { formAuthentication.SetAuthCookie(this.HttpContext, UserAuthenticationTicketBuilder.CreateAuthenticationTicket( user)); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } } } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(form); }
public void Cannot_Login_With_Wrong_UserCrdential() { //Arrange userRepository.Setup(x => x.Get(It.IsAny<Expression<Func<User, bool>>>())).Returns((User)null); LogOnFormModel logon = new LogOnFormModel(); logon.UserName = "******"; logon.Password = "******"; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); //Act var result = controller.Login(logon, "http://localhost:50521") as ViewResult; //Assert Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.AreEqual("Login", result.ViewName); }
public void Cannot_Login_With_Empty_UserCrdential() { //Arrange LogOnFormModel logon = new LogOnFormModel(); logon.UserName = string.Empty; logon.Password = string.Empty; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); // The MVC pipeline doesn't run, so binding and validation don't run. controller.ModelState.AddModelError("", "UserName and Password Should Provide"); //Act var result = controller.Login(logon, "http://localhost:50521") as ViewResult; //Assert Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.AreEqual("Login", result.ViewName); }
public void Cannot_Login_With_Wrong_UserCrdential() { //Arrange userRepository.Setup(x => x.Get(It.IsAny <Expression <Func <User, bool> > >())).Returns((User)null); LogOnFormModel logon = new LogOnFormModel(); logon.UserName = "******"; logon.Password = "******"; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); //Act var result = controller.Login(logon, "http://localhost:50521") as ViewResult; //Assert Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.AreEqual("Login", result.ViewName); }
public void Login_Success_Redirects_To_Home() { //Arrange var routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); var returnUrl = new Uri("http://efmvc.codeplex.com"); Mock <HttpRequestBase> request = new Mock <HttpRequestBase>(); Mock <HttpResponseBase> response = new Mock <HttpResponseBase>(); Mock <HttpContextBase> context = new Mock <HttpContextBase>(); context.SetupGet(x => x.Request).Returns(request.Object); context.SetupGet(x => x.Response).Returns(response.Object); request.Setup(x => x.Url).Returns(new Uri("http://*****:*****@gmail.com", UserId = 1, FirstName = "Shiju", LastName = "Var", DateCreated = DateTime.Now, Password = "******", RoleId = 2 }; userRepository.Setup(x => x.Get(It.IsAny <Expression <Func <User, bool> > >())).Returns(user); LogOnFormModel logon = new LogOnFormModel(); logon.UserName = user.Email; logon.Password = "******"; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); //Act controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes); var actual = controller.Login(logon, returnUrl.AbsoluteUri); //Assert Assert.IsInstanceOfType(typeof(RedirectToRouteResult), actual, "Wrong Type"); var result = (RedirectToRouteResult)actual; Assert.AreEqual("Home", result.RouteValues["controller"]); Assert.AreEqual("Index", result.RouteValues["action"]); }
public void Cannot_AjaxLogin_With_Empty_UserCrdential() { //Arrange LogOnFormModel logon = new LogOnFormModel(); logon.UserName = string.Empty; logon.Password = string.Empty; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); // The MVC pipeline doesn't run, so binding and validation don't run. controller.ModelState.AddModelError("", "UserName and Password Should Provide"); //Act var actual = controller.JsonLogin(logon, "http://localhost:50521"); //Assert // Assert.IsInstanceOfType(typeof(JsonResult), actual, "Wrong Type"); var result = (JsonResult)actual; bool success = (bool) (result.Data.GetType().GetProperty("success")).GetValue(result.Data, null); Assert.AreEqual(false, success); }
public ActionResult LogOn(LogOnFormModel logOnFormModel, string permanent, string returnUrl) { if (ModelState.IsValid) { var presentation = presentations .FindOne(p => p.Permanent == "Ruby31"); var attendee = (presentation ?? new Presentation()).Attendees.SingleOrDefault(a => a.Email == logOnFormModel.Email && a.Password == logOnFormModel.Password); if (attendee == null) { return(View(logOnFormModel)); } var ticket = new FormsAuthenticationTicket(1, attendee.Name, DateTime.Now, DateTime.Now.AddMinutes(30), false, attendee.Email); var encTicket = FormsAuthentication.Encrypt(ticket); Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); return(Redirect(returnUrl)); } return(View(logOnFormModel)); }
public async Task <ActionResult> Login(LogOnFormModel form, string returnUrl) { if (ModelState.IsValid) { var user = this.userRepository.Get(x => x.Email.ToUpper() == form.Email.ToUpper() && Md5Encrypt.Md5EncryptPassword(form.Password) == x.PasswordHash); if (user != null) { FNHMVCUser appUser = new FNHMVCUser(user); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, RedirectUri = returnUrl }, await appUser.GenerateUserIdentityAsync(userManager)); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } return(View(form)); }
public void Login_Success_Redirects_To_Home() { //Arrange var routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); var returnUrl = new Uri("http://efmvc.codeplex.com"); Mock<HttpRequestBase> request = new Mock<HttpRequestBase>(); Mock<HttpResponseBase> response = new Mock<HttpResponseBase>(); Mock<HttpContextBase> context = new Mock<HttpContextBase>(); context.SetupGet(x => x.Request).Returns(request.Object); context.SetupGet(x => x.Response).Returns(response.Object); request.Setup(x => x.Url).Returns(new Uri("http://*****:*****@gmail.com", UserId = 1, FirstName = "Shiju", LastName = "Var", DateCreated = DateTime.Now, Password = "******", RoleId = 2 }; userRepository.Setup(x => x.Get(It.IsAny<Expression<Func<User, bool>>>())).Returns(user); LogOnFormModel logon = new LogOnFormModel(); logon.UserName = user.Email; logon.Password = "******"; AccountController controller = new AccountController(commandBus.Object, userRepository.Object, formsAuthentication.Object); //Act controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes); var actual = controller.Login(logon, returnUrl.AbsoluteUri); //Assert Assert.IsInstanceOfType(typeof(RedirectToRouteResult), actual ,"Wrong Type"); var result = (RedirectToRouteResult)actual; Assert.AreEqual("Home", result.RouteValues["controller"]); Assert.AreEqual("Index", result.RouteValues["action"]); }