예제 #1
0
 public ActionResult Login(string content)
 {
     var model = new LoginViewModel();
     if (!String.IsNullOrEmpty(content))
     {
         return PartialView(model);
     }
     else
     {
         return View(model);
     }
 }
예제 #2
0
 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);
 }
예제 #3
0
            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());
            }
예제 #4
0
            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);
            }
예제 #5
0
            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());
            }
예제 #6
0
            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());
            }
예제 #7
0
            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" });
            }
예제 #8
0
            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");
            }
예제 #9
0
            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());
            }
예제 #10
0
            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());
            }