예제 #1
0
        public async Task RegisterSuccsess()
        {
            var userStore = new Mock<UserStore<User>>();
            Mock<ApplicationUserManager> userManager = new Mock<ApplicationUserManager>(userStore.Object);

            var registerBindingModel = new RegisterBindingModel()
           {
               Email = "*****@*****.**",
               Password = "******",
               ConfirmPassword = "******"
           };

            userManager.Setup(x => x.CreateAsync(It.IsAny<User>(), registerBindingModel.Password)).ReturnsAsync(IdentityResult.Success);

            userManager.Setup(x => x.AddToRoleAsync(It.IsAny<string>(), "Guest"));

            var accountController = new AccountController(userManager.Object,null) { Request = new HttpRequestMessage() };

            IHttpActionResult register = accountController.Register(registerBindingModel).Result;

            Assert.IsInstanceOfType(register, typeof(OkResult));
        }
예제 #2
0
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            User user = new User()
            {
                UserName = model.Email,
                Email = model.Email,
                Image = model.Image.ToRelativePath()
            };

            IdentityResult createUserResult = await UserManager.CreateAsync(user, model.Password);


            if (!createUserResult.Succeeded)
            {
                return GetErrorResult(createUserResult);
            }

            var addRoleResult = await UserManager.AddToRoleAsync(user.Id, UserRoles.User);

            return !addRoleResult.Succeeded ? GetErrorResult(addRoleResult) : Ok();
        }