예제 #1
0
        public async Task <IHttpActionResult> AddUser(AddUpdateUserRequest user)
        {
            if (ModelState.IsValid)
            {
                var userToCreate = new ApplicationUser {
                    UserName = user.UserName, Email = user.Email
                };
                var result = await UserManager.CreateAsync(userToCreate, user.Password);

                if (result.Succeeded)
                {
                    await this.UserManager.AddToRoleAsync(userToCreate.Id, user.RoleName);

                    var role = await this.RoleManager.FindByNameAsync(user.RoleName);

                    return(Ok(new UserResponse(userToCreate, role, string.Equals(userToCreate.Id, this.User.Identity.GetUserId(), StringComparison.OrdinalIgnoreCase))));
                }
                else
                {
                    return(BadRequest());
                }

                //result.Errors
                //AddErrors(result);
            }

            return(StatusCode(HttpStatusCode.InternalServerError));
        }
예제 #2
0
        public async Task <IHttpActionResult> UpdateUser(string id, AddUpdateUserRequest user)
        {
            if (string.Equals(id, this.User.Identity.GetUserId(), StringComparison.OrdinalIgnoreCase))
            {
                return(BadRequest("Currently authenticated user is not allowed to modify themselves."));
            }

            var userToUpdate = await this.IdentityDb.Users.SingleOrDefaultAsync(x => x.Id == id);

            if (userToUpdate == null)
            {
                return(NotFound());
            }

            if (!string.IsNullOrEmpty(user.RoleName))
            {
                var allRolesForUser = await this.UserManager.GetRolesAsync(userToUpdate.Id);

                await this.UserManager.RemoveFromRolesAsync(userToUpdate.Id, allRolesForUser.ToArray());

                this.UserManager.AddToRole(userToUpdate.Id, user.RoleName);
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }