예제 #1
0
        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)));
        }
예제 #2
0
        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());
        }