public async Task <ProcessResult <AppUserDTO> > CreateAsync(AppUserForRegister model) { async Task <AppUserDTO> action() { var now = DateTime.UtcNow; var newId = Guid.NewGuid(); var countUserExists = await _repoWrapper.AppUser.FindByCondition(u => u.UserName.Equals(model.UserName) || u.Email.Equals(model.Email)).CountAsync(); if (countUserExists > 0) { throw new InvalidOperationException("Username or Email is exists"); } var appRoles = await _repoWrapper.AppRole.FindAll().ToListAsync(); var userRoles = new List <AppUserRole>(); if (model.RoleIds != null) { if (model.RoleIds.Except(appRoles.Select(r => r.Id)).Any()) { throw new InvalidOperationException("Invalid Role"); } model.RoleIds.ToList().ForEach(rId => { userRoles.Add(new AppUserRole { UserId = newId, RoleId = rId }); }); } using var hmac = new HMACSHA512(); var user = new AppUser { Id = newId, UserName = model.UserName.ToLower(), Email = model.Email.ToLower(), DateOfBirth = model.DateOfBirth, Gender = model.Gender, PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes("12345678")), PasswordSalt = hmac.Key, Status = true, CreateDate = now, CreateBy = CurrentUser.UserName, LastActive = now, Version = 1, AppUserRoles = userRoles }; _repoWrapper.AppUser.Create(user); return(await _repoWrapper.SaveAsync() > 0 ? _mapper.Map <AppUserDTO>(user) : throw new InvalidOperationException("Save fail")); } return(await Process.RunAsync(action)); }
public async Task <ProcessResult <LoginResponse> > RegisterAsync(AppUserForRegister model) { async Task <LoginResponse> action() { var now = DateTime.UtcNow; var newId = Guid.NewGuid(); var countUserExists = await _repoWrapper.AppUser.FindByCondition(u => u.UserName.Equals(model.UserName) || u.Email.Equals(model.Email)).CountAsync(); if (countUserExists > 0) { throw new InvalidOperationException("Username or Email is exists"); } const string registerRoleName = "Member"; var memberRole = await _repoWrapper.AppRole.FindAppRoleByName(registerRoleName); using var hmac = new HMACSHA512(); var userRoles = new List <AppUserRole> { new AppUserRole { UserId = newId, RoleId = memberRole.Id } }; var user = new AppUser { Id = newId, UserName = model.UserName.ToLower(), Email = model.Email.ToLower(), DateOfBirth = model.DateOfBirth, Gender = model.Gender, PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(model.Password)), PasswordSalt = hmac.Key, Status = true, CreateDate = now, CreateBy = model.UserName.ToLower(), LastActive = now, Version = 1, AppUserRoles = userRoles }; _repoWrapper.AppUser.Create(user); return(await _repoWrapper.SaveAsync() > 0 ? new LoginResponse { UserName = user.UserName, Token = await _tokenService.CreateTokenAsync(user), Roles = new string[] { registerRoleName } } : throw new InvalidOperationException("Save fail")); } return(await Process.RunAsync(action)); }
public async Task <ActionResult <AppUserDTO> > RegisterByAdminAsync(AppUserForRegister model) { var result = await _appUserService.CreateAsync(model); if (result.Succeed == false) { return(BadRequest(new ErrorResponse(HttpStatusCode.BadRequest, "Register fail", result.Errors))); } return(Ok(result.Value)); }
public async Task <ActionResult <LoginResponse> > RegisterAsync(AppUserForRegister model) { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList(); return(BadRequest(new ErrorResponse(HttpStatusCode.BadRequest, "Validation error", errors))); } var result = await _appUserService.RegisterAsync(model); return(result.Succeed ? Ok(result.Value) : BadRequest(new ErrorResponse(HttpStatusCode.BadRequest, "Register fail", result.Errors))); }