public IActionResult Put([BindRequired, FromBody] AltSourceNewUserDto input) { try { UserInputValidation.ValidateUsername(input.Username); UserInputValidation.ValidatePassword(input.Password); UserInputValidation.ValidateName(input.FirstName); UserInputValidation.ValidateName(input.LastName); UserInputValidation.ValidateAddress(input.Address); var encodedAddress = HtmlEncoder.Default.Encode(input.Address); // ReSharper disable once UnusedVariable var result = Users.CreateAltSourceUser(_userManager, input.Username, input.Password, input.FirstName, input.LastName, encodedAddress, false) .Result; if (!result.Succeeded) { return(BadRequest(result.CompileErrorMessage())); } } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Ok()); }
/// <summary> /// Creates a new user on the Identity Server, with a role of Client. Users created with this /// method can be used to query/credit/debit the accounts owned by this user, but they cannot /// create new users or otherwise modify Identity data. /// </summary> /// <param name="username">The new user's username; legal values constrained by the IdentityServer settings.</param> /// <param name="password">The new user's password; legal values constrained by the IdentityServer settings.</param> /// <param name="firstName">The new user's given name. </param> /// <param name="lastName">The new user's family name.</param> /// <returns></returns> public async Task CreateUser(string username, string password, string firstName, string lastName) { var newUser = new AltSourceNewUserDto(username, password, firstName, lastName, false); var jsonNewUser = JsonConvert.SerializeObject(newUser); var content = new StringContent(jsonNewUser, Encoding.UTF8, "application/json"); var response = await this.InvokeApiAsync(Post, $"api/manage/user/create", content); if (!response.IsSuccessStatusCode) { var msg = response.Content.ReadAsStringAsync(); throw new HttpRequestException($"{response.ReasonPhrase} - {msg.Result}"); } }
/// <summary> /// Creates a new Identity with the specified username, password, and name. Also creates a corresponding /// User on the Accounts system and links it to the new Identity via the <see cref="ApplicationClaimTypes.ACCOUNTS_USERNAME"/> /// claim. /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="isAdmin"></param> /// <param name="userCredentialsCallback"></param> /// <returns></returns> public async Task CreateUser(string username, string password, string firstName, string lastName, bool isAdmin, UserCredentialsCallback userCredentialsCallback) { var accessToken = await GetCurrentAccessToken(userCredentialsCallback); var newUser = new AltSourceNewUserDto(username, password, firstName, lastName, isAdmin); var jsonNewUser = JsonConvert.SerializeObject(newUser); var content = new StringContent(jsonNewUser, Encoding.UTF8, "application/json"); using (var identityApi = new HttpClient { BaseAddress = _identityServerUrl }) { identityApi.SetBearerToken(accessToken); var requestUrl = (isAdmin ? "api/manage/admin/create" : "api/manage/user/create"); var result = await identityApi.PutAsync(requestUrl, content); if (!result.IsSuccessStatusCode) { var msg = result.Content.ReadAsStringAsync(); throw new HttpRequestException($"{result.ReasonPhrase} - {msg.Result}"); } } using (var accountsApi = new AccountsApiClient(_accountsApiUrl, accessToken)) { if (!await accountsApi.CreateUser(username)) { throw new HttpRequestException($"Failed to create user '{username}' on the Accounts system."); } } }