public async Task <int> CreateUserRegistrationAsync([FromBody] CreatedUserRq model) { try { var entity = new User() { UserName = model.UserName, PasswordHash = model.Password, Email = model.Email, PhoneNumber = model.PhoneNumber }; string password = model.Password; var identityResult = await _userManagement.CreateAsync(entity, password); if (!identityResult.Succeeded) { throw CreateException(identityResult.Errors); } var rs = await _userManagement.AddToRoleAsync(entity, model.Role); return(entity.Id); } catch (Exception e) { throw e; } }
public async Task <int> CreateUserAsync(CreatedUserRq model, UserIdentity <int> issuer = null) { try { model.Status = true; var user = _mapper.Map <User>(model); if (issuer != null) { user.CreateBy(issuer).UpdateBy(issuer); } //string password = AutoGenerate.AutoGeneratePassword(8, true, true); string password = "******"; var identityResult = await _userManagement.CreateAsync(user, password); if (!identityResult.Succeeded) { throw CreateException(identityResult.Errors); } await _userManagement.AddToRoleAsync(user, ROLE_CONSTANT.EMPLOYEE); EmailTemplate emailTemplate = await _emailTemplateRepository.GetEmailTemplateByNameAsync("NewUserEmail"); emailTemplate.EmailContent = emailTemplate.EmailContent.Replace("#email", model.Email); emailTemplate.EmailContent = emailTemplate.EmailContent.Replace("#username", model.UserName); emailTemplate.EmailContent = emailTemplate.EmailContent.Replace("#password", password); try { await _emailSender.SendEmailAsync(model.Email, emailTemplate.EmailSubject, emailTemplate.EmailContent, true); } catch (Exception e) { throw e; } return(user.Id); } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> CreateUserAsync([FromBody] CreatedUserRq model) { try { if (!ModelState.IsValid) { Microsoft.AspNetCore.Mvc.ModelBinding.ModelErrorCollection modelErrors = new Microsoft.AspNetCore.Mvc.ModelBinding.ModelErrorCollection(); foreach (var entry in ModelState.Values) { foreach (var error in entry.Errors) { modelErrors.Add(error); } } return(BadRequest(modelErrors)); } return(Ok(await _userService.CreateUserAsync(model))); } catch (Exception exception) { return(BadRequest(exception.Message)); } }