public void Test_should_set_UserPrinciple_if_User_is_authenticated() { var mockHttpContext = new Mock<HttpContextBase>(); var genericPrincipal = new GenericPrincipal(new GenericIdentity("username"), new string[]{}); mockHttpContext.SetupProperty(c => c.User, genericPrincipal); var controllerContext = new ControllerContext() { HttpContext = mockHttpContext.Object }; using (var dbContext = new HeliosDbContext()) { var dbUser = new User() { UserName = "******", IsAdmin = true }; dbUser.SetPassword("secret"); dbContext.Users.Add(dbUser); dbContext.Commit(); var stubController = new StubController(dbContext) { ControllerContext = controllerContext }; stubController.CallOnAuthorization(new AuthorizationContext() { HttpContext = controllerContext.HttpContext }); Assert.Equal(true, stubController.User is UserPrincipal); Assert.Equal("username", stubController.User.Identity.Name); Assert.Equal(true, stubController.User.IsInRole("admin")); } }
public ActionResult Create(SignUpViewModel signUpViewModel) { if (this.ModelState.IsValid) { var newUser = new User() { UserName = signUpViewModel.UserName, RequiredToChangePassword = true }; newUser.SetPassword(signUpViewModel.Password); Users.Add(newUser); return RedirectToRoute(new { controller = "Home", action = "Index" }); } return View("New"); }
private static void SaveUserInDb(HeliosDbContext context, string userName, string password, bool requiredToChangePassword = false) { var dbUser = new User() { UserName = userName, RequiredToChangePassword = requiredToChangePassword }; dbUser.SetPassword(password); context.Users.Add(dbUser); context.Commit(); }
private User CreateUser(string userName = "******", string password = "******") { var user = new User { UserName = userName }; user.SetPassword(password); return user; }