コード例 #1
0
        public async Task <RenewAuthenticationCommandResult> Execute(RenewAuthenticationCommandArgs args)
        {
            var tokenHandler = new JwtSecurityTokenHandler();

            var principle = tokenHandler.ValidateToken(args.RefreshToken, _tokenValidationParameters, out _);
            var id        = principle.FindFirst(JwtRegisteredClaimNames.Sid).Value;
            var user      = await _userManager.FindByIdAsync(id);

            return(new RenewAuthenticationCommandResult
            {
                AccessToken = tokenHandler.WriteToken(_tokensFactory.GenerateAccessToken(user)),
                RefreshToken = tokenHandler.WriteToken(_tokensFactory.GenerateRefreshToken(user))
            });
        }
コード例 #2
0
        public async Task <AuthenticateUserCommandResult> Execute(AuthenticateUserCommandArgs args)
        {
            var user = await _userManager.FindByEmailAsync(args.Email);

            if (user == null || !await _userManager.CheckPasswordAsync(user, args.Password))
            {
                throw new ServiceException("User not found or password is incorrect");
            }

            var accessToken  = _tokensFactory.GenerateAccessToken(user);
            var refreshToken = _tokensFactory.GenerateRefreshToken(user);

            var tokenHandler = new JwtSecurityTokenHandler();

            return(new AuthenticateUserCommandResult
            {
                AccessToken = tokenHandler.WriteToken(accessToken),
                RefreshToken = tokenHandler.WriteToken(refreshToken)
            });
        }