public ActionResult Login(string content) { var model = new LoginViewModel(); if (!String.IsNullOrEmpty(content)) { return PartialView(model); } else { return View(model); } }
public ActionResult Login(LoginViewModel model, string returnUrl, bool isAjaxRequest) { if (ModelState.IsValid) { User user = AuthService.LogIn(model.Email, model.Password); if (user != null) { IEnumerable<Role> roles = user.Roles ?? Enumerable.Empty<Role>(); TokenService.SetAuthCookie(model.Email, createPersistentCookie: model.RememberMe, roles: roles.Select(r => r.RoleName)); return CreateSuccessResult(returnUrl, isAjaxRequest); } else { ModelState.AddModelError(String.Empty, "Either there is no user with that email address, or you entered the wrong password."); } } return CreateErrorResult(model, isAjaxRequest); }
public void WithInvalidModelReturnsError_Ajax() { // Arrange var c = CreateController(); c.ModelState.AddModelError("", "Test"); var model = new LoginViewModel(); // Act var result = c.Login(model, returnUrl: null, isAjaxRequest: true); // Assert ActionAssert.IsJsonResult(result, new { success = false, errors = new[] { "Test" } }); Assert.Contains("Test", c.ModelState.AllErrors()); }
public void WithValidModelAndUserInfoSetsPersistentAuthTokenIfRememberMeSet() { // Arrange var model = new LoginViewModel() { Email = "*****@*****.**", Password = "******", RememberMe = true }; var c = CreateController(); c.AuthService.CreateUser("*****@*****.**", "Real User", "password"); // Act var result = c.Login(model, returnUrl: null, isAjaxRequest: false); // Assert ActionAssert.IsRedirectResult(result, "/app/Home/Index"); Assert.Equal("*****@*****.**", c.TestTokenService.UserName); Assert.True(c.TestTokenService.Persistent); }
public void WithInvalidModelReturnsError() { // Arrange var c = CreateController(); c.ModelState.AddModelError("", "Test"); var model = new LoginViewModel(); // Act var result = c.Login(model, returnUrl: null, isAjaxRequest: false); // Assert ActionAssert.IsViewResult(result, model); Assert.Contains("Test", c.ModelState.AllErrors()); }
public void WithValidModelAndUserInfoSetsAuthToken() { // Arrange var model = new LoginViewModel() { Email = "*****@*****.**", Password = "******" }; var c = CreateController(); c.AuthService.CreateUser("*****@*****.**", "Real User", "password"); var user = c.AuthService.Data.Users.Where(u => u.Email == "*****@*****.**").Single(); user.Roles = new List<Role>() { new Role() { RoleName = "Role1" }, new Role() { RoleName = "Role2" }, new Role() { RoleName = "Role3" } }; // Act var result = c.Login(model, returnUrl: null, isAjaxRequest: false); // Assert ActionAssert.IsRedirectResult(result, "/app/Home/Index"); Assert.Equal("*****@*****.**", c.TestTokenService.UserName); Assert.False(c.TestTokenService.Persistent); Assert.Equal(new[] { "Role1", "Role2", "Role3" }, c.TestTokenService.Roles.ToArray()); }
public void WithValidModelAndUserInfoReturnsRedirectToReturnUrlIfLocal_Ajax() { // Arrange var model = new LoginViewModel() { Email = "*****@*****.**", Password = "******" }; var c = CreateController(); c.AuthService.CreateUser("*****@*****.**", "Real User", "password"); // Act var result = c.Login(model, returnUrl: "/app/Foo/Bar", isAjaxRequest: true); // Assert ActionAssert.IsJsonResult(result, new { success = true, redirect = "/app/Foo/Bar" }); }
public void WithValidModelAndUserInfoReturnsRedirectToHomeIfReturnUrlNotLocal() { // Arrange var model = new LoginViewModel() { Email = "*****@*****.**", Password = "******" }; var c = CreateController(); c.AuthService.CreateUser("*****@*****.**", "Real User", "password"); // Act var result = c.Login(model, returnUrl: "http://www.microsoft.com", isAjaxRequest: false); // Assert ActionAssert.IsRedirectResult(result, "/app/Home/Index"); }
public void WithInvalidUserNameReturnsError_Ajax() { // Arrange var model = new LoginViewModel() { Email = "*****@*****.**", Password = "******" }; var c = CreateController(); // Act var result = c.Login(model, returnUrl: null, isAjaxRequest: true); // Assert ActionAssert.IsJsonResult(result, new { success = false, errors = new[] { "Either there is no user with that email address, or you entered the wrong password." } }); Assert.Contains("Either there is no user with that email address, or you entered the wrong password.", c.ModelState.AllErrors()); }
public void WithInvalidPasswordReturnsError() { // Arrange var model = new LoginViewModel() { Email = "*****@*****.**", Password = "******" }; var c = CreateController(); c.AuthService.CreateUser("*****@*****.**", "Real User", "123456"); // Act var result = c.Login(model, returnUrl: null, isAjaxRequest: false); // Assert ActionAssert.IsViewResult(result, model); Assert.Contains("Either there is no user with that email address, or you entered the wrong password.", c.ModelState.AllErrors()); }