/// <summary> /// Add a password to a given user if they dont have one /// </summary> /// <param name="request">Instance of GetLoginsRequest</param> /// <returns>Instance of GetLoginsResponse</returns> public async Task <AddPasswordResponse> AddPasswordAsync(AddPasswordRequest request) { AddPasswordResponse response = new AddPasswordResponse(); try { IdentityResult result = await UserManager.AddPasswordAsync(request.UserId.ToString(), request.NewPassword); if (!result.Succeeded) { foreach (string item in response.Errors) { response.Errors.Add(item); } response.Success = false; } else { response.Success = true; } } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return(response); }
public async Task <ActionResult> Manage(ManageUserViewModel model) { bool hasPassword = HasPassword(); ViewBag.HasLocalPassword = hasPassword; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasPassword) { if (ModelState.IsValid) { ChangePasswordResponse response = await this.Membership.ChangePasswordAsync(new ChangePasswordRequest() { UserId = new Guid(User.Identity.GetUserId()), OldPassword = model.OldPassword, NewPassword = model.NewPassword }); if (response.Success) { return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess })); } else { AddErrors(response.Errors); } } } else { // User does not have a password so remove any validation errors caused by a missing OldPassword field ModelState state = ModelState["OldPassword"]; if (state != null) { state.Errors.Clear(); } if (ModelState.IsValid) { AddPasswordResponse response = await this.Membership.AddPasswordAsync(new AddPasswordRequest() { UserId = new Guid(User.Identity.GetUserId()), NewPassword = model.NewPassword }); if (response.Success) { return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess })); } else { AddErrors(response.Errors); } } } // If we got this far, something failed, redisplay form return(View(model)); }
/// <summary> /// Add a password to a given user if they dont have one /// </summary> /// <param name="request">Instance of GetLoginsRequest</param> /// <returns>Instance of GetLoginsResponse</returns> public async Task<AddPasswordResponse> AddPasswordAsync(AddPasswordRequest request) { AddPasswordResponse response = new AddPasswordResponse(); try { IdentityResult result = await UserManager.AddPasswordAsync(request.UserId.ToString(), request.NewPassword); if (!result.Succeeded) { foreach (string item in response.Errors) response.Errors.Add(item); response.Success = false; } else { response.Success = true; } } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return response; }