Esempio n. 1
0
 public async Task<ActionResult> Create(CreateModel model)
 {
     if (ModelState.IsValid)
     {
         var result = await repository.CreateUser(model);
         if (result.Succeeded)
         {
             return RedirectToAction("ManageUsers");
         }
         AddErrorsFromResult(result);
     }
     return View(model);
 }
Esempio n. 2
0
        public async Task<ActionResult> SignUp(CreateModel model)
        {
            if (ModelState.IsValid)
            {
                if (System.Web.HttpContext.Current.User != null && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    ModelState.AddModelError("", "Du er allerede logget på");
                }
                AppUser userExists = UserManager.FindByName(model.Name);
                if (userExists != null)
                {
                    ModelState.AddModelError("", "Brugernavnet eksisterer allerede. Vælg venligst et nyt");
                }
                userExists = UserManager.FindByEmail(model.Email);
                if (userExists != null)
                {
                    ModelState.AddModelError("", "Emailen eksisterer allerede. Vælg venligst en ny");
                }

                if (ModelState.IsValid)
                {
                    var user = new AppUser
                    {
                        UserName = model.Name,
                        PasswordHash = new PasswordHasher().HashPassword(model.Password),
                        Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(user);
                    if (!result.Succeeded)
                    {
                        return View("Error", new List<string> { "Brugernavn eller Email er i brug. Hvis du tidligere har logget via google eller med facebook, så prøv venligst samme metode" });
                    }

                    ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
                    AuthManager.SignOut();
                    AuthManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = false
                    }, ident);
                    return RedirectToAction("Index", "Forum");
                }
            }
            return View(model);
        }
        public void TestPostCreateValidModelAndSuccess()
        {
            //Arrange
            CreateModel validModel = new CreateModel
            {
                Name = "ValidName",
                Email = "*****@*****.**",
                Password = "******"
            };
            Mock<IAdminRepository> mockRepo = new Mock<IAdminRepository>();
            mockRepo.Setup(x => x.CreateUser(It.IsAny<CreateModel>())).
                Returns(Task.FromResult<IdentityResult>(IdentityResult.Success));
            AdminController target = new AdminController(mockRepo.Object);

            //Act
            Task<ActionResult> result = target.Create(validModel) as Task<ActionResult>;

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(target.ModelState.IsValid);
            Assert.IsInstanceOfType(result.Result, typeof (RedirectToRouteResult));
            RedirectToRouteResult redirectRouteResult = result.Result as RedirectToRouteResult;
            Assert.AreEqual("ManageUsers", redirectRouteResult.RouteValues["Action"]);
        }