コード例 #1
0
        public async Task <TokenResponse> RegisterAsync(RegistrationRequest registrationRequest)
        {
            if (await AccountRepository.ExistsWithEmailAsync(registrationRequest.Email))
            {
                return(TokenResponse.Unauthorized());
            }

            var passwordHash = await HashGenerator.GenerateSaltedHash(registrationRequest.Password);

            var account = new Account
            {
                Id           = Guid.NewGuid(),
                Email        = registrationRequest.Email,
                FullName     = registrationRequest.FullName,
                RoleId       = (int)UserRole.User,
                PasswordHash = passwordHash
            };

            await AccountRepository.CreateAsync(account);

            await AccountRepository.SaveChangesAsync();

            var token = JwtGenerator.GenerateToken(account.Id);

            return(TokenResponse.Success(token));
        }
コード例 #2
0
        public async Task <TokenResponse> LogInAsync(AuthenticationRequest authenticationRequest)
        {
            var account = await AccountRepository.GetWithEmailAsync(authenticationRequest.Email);

            if (account != null)
            {
                var passwordHash = await HashGenerator.GenerateSaltedHash(authenticationRequest.Password);

                if (passwordHash.SequenceEqual(account.PasswordHash))
                {
                    var role  = (UserRole)account.RoleId;
                    var token = JwtGenerator.GenerateToken(account.Id, role);

                    return(TokenResponse.Success(token));
                }
            }

            return(TokenResponse.Unauthorized());
        }