private async Task <Contract.User> GetUserContractAsync(IdentityUser identityUser) { var user = identityUser.ToContract(); var roles = await _userManager.GetRolesAsync(identityUser).ConfigureAwait(false); return(user.WithToken(GenerateToken(user.Username, user.Email, roles))); }
public async Task <Contract.User> CreateUserAsync(Contract.User user, string password) { var identityUser = await GetIdentityUserByUserNameAsync(user.Username).ConfigureAwait(false); if (identityUser != null) { throw new UserAlreadyExistException(user.Username); } var identityUserToCreate = new IdentityUser { Email = user.Email, UserName = user.Username }; identityUserToCreate.PasswordHash = _userManager.PasswordHasher.HashPassword(identityUserToCreate, password); var createResult = await _userManager.CreateAsync(identityUserToCreate).ConfigureAwait(false); if (!createResult.Succeeded) { throw new UserCreateException(createResult.Errors); } return(identityUserToCreate.ToContract()); }