public ActionResult Index()
        {
            ViewBag.Title = "Search";
            UpdateAccountBindingModel updateModel = new UpdateAccountBindingModel();

            return(View(updateModel));
        }
        //[Route("UpdateAccount")]
        public async Task <HttpResponseMessage> UpdateAccount(UpdateAccountBindingModel model, string userId)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var result = await updateManager.UpdateAccountAsync(userId, model.Email, model.PhoneVisibility
                                                                , model.UserName, model.UserVisibility);

            if (!result)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "update failed"));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, "updated successfully"));
        }
Example #3
0
        public async Task <IHttpActionResult> UpdateAccount(UpdateAccountBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            ISS.Authentication.Domain.Models.User user = await UserManager.FindByIdAsync(new Guid(User.Identity.GetUserId()));

            if ((model.Email != user.Email) && (model.Password != null) && (model.Password.Trim() != ""))
            {
                if (await UserManager.CheckPasswordAsync(user, model.Password) == true)
                {
                    user.Email          = model.Email;
                    user.UserName       = user.Email;
                    user.EmailConfirmed = false;
                }
                else
                {
                    return(BadRequest("The provided password was incorrect"));
                }
            }
            else
            {
                return(BadRequest("You must provide your password in order to change your email address"));
            }
            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            IdentityResult result = UserManager.Update(user);

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

            return(Ok());
        }
Example #4
0
        public async Task <IHttpActionResult> UpdateAccount(UpdateAccountBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (model.Roles.Length == 0)
            {
                return(BadRequest("Нет ни одной роли. Невозможно создать учетную запись без ролей."));
            }

            // find user
            var user = await UserManager.FindByIdAsync(model.AccountId);

            if (user == null)
            {
                return(NotFound());
            }
            // update user
            user.LoginDisabled = model.LoginDisabled;
            user.Email         = model.Email;

            IdentityResult userUpdate = await UserManager.UpdateAsync(user);

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


            var roles            = RoleManager.Roles;
            var currentUserRoles = user.Roles.Select(x => x.RoleId);
            var currentRoles     = new List <string>();

            foreach (var currentRoleId in currentUserRoles)
            {
                var roleName = RoleManager.FindById(currentRoleId).Name;
                if (!string.IsNullOrEmpty(roleName))
                {
                    currentRoles.Add(roleName);
                }
            }

            var rolesToRemove = currentRoles.Except(model.Roles).ToArray();
            var rolesToAdd    = model.Roles.Except(currentRoles).ToArray();

            var roleAddition = await UserManager.AddToRolesAsync(user.Id, rolesToAdd);

            var roleRemoval = await UserManager.RemoveFromRolesAsync(user.Id, rolesToRemove);

            if (!roleAddition.Succeeded)
            {
                return(GetErrorResult(roleAddition));
            }
            if (!roleRemoval.Succeeded)
            {
                return(GetErrorResult(roleRemoval));
            }

            // password
            if (!string.IsNullOrEmpty(model.Password))
            {
                var passwordChange = await UserManager.ForceNewPasswordAsync(user, model.Password);

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

            return(Ok());
        }