コード例 #1
0
        public async Task <TokenResponse> RunAsync(UserInfoRequest request)
        {
            var user = await _userAuthRepository.ValidateCredentials(request);

            if (user == null)
            {
                return(null);
            }

            var clains = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName)
            };

            var accessToken = await _tokenGenerate.GenerateAccessToken(clains);

            var refreshToken = await _tokenGenerate.GenerateRefreshToken();

            user.RefreshToken       = refreshToken;
            user.AcessToken         = accessToken;
            user.RefreshTokenExpire = DateTime.Now.AddDays(Convert.ToDouble(_configuration.GetSection("TokenExtensions:DaysToExpiry").Value));

            await _userAuthRepository.RefresUserInfo(user);

            var createDate = DateTime.Now;
            var expireDate = createDate.AddMinutes(Convert.ToDouble(_configuration.GetSection("TokenExtensions:Minutes").Value));


            return(new TokenResponse
            {
                Authenticated = true,
                AccessToken = accessToken,
                Created = createDate.ToString(DATE_FORMATE),
                Expiration = expireDate.ToString(),
                RefreshToken = refreshToken
            });
        }
コード例 #2
0
        public async Task <TokenResponse> RunAsync(TokenResponse request)
        {
            var accessToken  = request.AccessToken;
            var refreshToken = request.RefreshToken;
            var principal    = await _tokenGenerate.GetPrincipalFromExpiredToken(accessToken);

            var user = await _unitOfWork.UserAuthRepository.ValidateCredentials(principal.Identity.Name);

            if (user == null ||
                user.RefreshToken != refreshToken ||
                user.RefreshTokenExpire <= DateTime.Now)
            {
                return(null);
            }

            accessToken = await _tokenGenerate.GenerateAccessToken(principal.Claims);

            refreshToken = await _tokenGenerate.GenerateRefreshToken();

            user.RefreshToken = refreshToken;

            await _unitOfWork.UserAuthRepository.RefresUserInfo(user);

            var createDate = DateTime.Now;
            var expireDate = createDate.AddMinutes(Convert.ToDouble(_configuration.GetSection("TokenExtensions:Minutes").Value));


            return(new TokenResponse
            {
                Authenticated = true,
                AccessToken = accessToken,
                Created = createDate.ToString(DATE_FORMATE),
                Expiration = expireDate.ToString(),
                RefreshToken = refreshToken
            });
        }