public async Task <IHttpActionResult> UnlockAccount(UnlockAccountModel model) { if (!ModelState.IsValid) { return(BadRequest()); } try { _web3Client = new Web3Geth($"http://{model.NodeUrl}:8545/"); var isAccountUnlocked = await _web3Client.Personal.UnlockAccount.SendRequestAsync(model.AccountAddress, model.PassPhrase, 100); return(Ok(isAccountUnlocked)); } catch (Exception) { BadRequest("Somthing went wrong"); } return(Ok()); }
public async Task <IActionResult> Post([FromBody] UnlockAccountModel model) { // Validate the request if (model == null) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(ApiResult.InvalidRequest())); } var result = new ApiResult(); // Validate the model if (ModelState.IsValid == false) { result.AddModelStateErrors(ModelState); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(result)); } // Validate the Captcha try { if (await ValidateRecaptcha(model.Recaptcha) == false) { result.Errors.Add(new ApiErrorItem() { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.InvalidCaptcha }); } } catch (Exception ex) { result.Errors.Add(new ApiErrorItem() { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.Generic, Message = ex.Message }); } if (result.HasErrors) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(result)); } // perform the user unlock try { using (var principalContext = AcquirePrincipalContext()) { var userPrincipal = AcquireUserPricipal(principalContext, model.Username); // Check if the user principal exists if (userPrincipal == null) { result.Errors.Add(new ApiErrorItem() { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.UserNotFound }); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(result)); } // Check if password change is allowed if (userPrincipal.UserCannotChangePassword) { throw new Exception(Settings.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed); } // Validate user credentials if (principalContext.ValidateCredentials(model.Username, model.CurrentPassword) == false) { throw new Exception(Settings.ClientSettings.Alerts.ErrorInvalidCredentials); } // Change the password via 2 different methods. Try SetPassword if ChangePassword fails. try { // Try by regular ChangePassword method userPrincipal.ChangePassword(model.CurrentPassword, model.NewPassword); } catch (Exception ex2) { // If the previous attempt failed, use the SetPassword method. if (Settings.PasswordChangeOptions.UseAutomaticContext == false) { userPrincipal.SetPassword(model.NewPassword); } else { throw ex2; } } userPrincipal.Save(); } } catch (Exception ex) { result.Errors.Add(new ApiErrorItem() { ErrorType = ApiErrorType.GeneralFailure, ErrorCode = ApiErrorCode.Generic, Message = ex.Message }); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(result)); } if (result.HasErrors) { Response.StatusCode = (int)HttpStatusCode.BadRequest; } return(Json(result)); }