Exemplo n.º 1
0
        private async Task <JsonWebToken> GenerateTokenAsync(List <Claim> claims, JwtOptions options)
        {
            claims.Add(new Claim(JwtRegisteredClaimNames.Nbf, $"{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}"));
            claims.Add(new Claim(JwtRegisteredClaimNames.Exp, $"{DateTimeOffset.UtcNow.AddMinutes(options.AccessExpireMins).ToUnixTimeSeconds()}"));

            // AccessToken
            var(accessToken, accessExpires) = CreateTokenCore(claims, options, JwtTokenType.AccessToken);

            // RefreshToken
            var(refreshToken, refreshExpires) = CreateTokenCore(claims, options, JwtTokenType.RefreshToken);

            if (options.Cache)
            {
                var clientType = claims.FirstOrDefault(d => d.Type == nameof(TokenEntityBase.ClientType)).Value;
                var userId     = claims.FirstOrDefault(d => d.Type == nameof(TokenEntityBase.UserId)).Value;

                var tokenEntry = CacheEntryCollection.GetTokenEntry(JwtTokenType.AccessToken, clientType, userId, (int)options.AccessExpireMins * 60);
                await _store.SetAsync <string>(tokenEntry, accessToken);

                tokenEntry = CacheEntryCollection.GetTokenEntry(JwtTokenType.RefreshToken, clientType, userId, (int)options.AccessExpireMins * 60);
                await _store.SetAsync <string>(tokenEntry, accessToken);
            }

            return(new JsonWebToken()
            {
                AccessToken = accessToken,
                RefreshToken = refreshToken,
                AccessExpires = accessExpires.ToUniversalTime().Ticks,
                RefreshExpires = refreshExpires.ToUniversalTime().Ticks//expires.ToJsGetTime().ParseByLong()
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// 校验token是否正确
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public virtual async Task <ClaimsPrincipal> ValidateTokenAsync(JwtTokenType jwtTokenType, string token, JwtOptions options = null)
        {
            if (options == null)
            {
                options = GetCurrentOptions();
            }
            ClaimsPrincipal principal = _tokenHandler.ValidateToken(token, options.GetValidationParameters(), out _);
            string          userId    = null;

            if (options.EnabledSignalR)
            {
                userId = principal.Claims.FirstOrDefault(d => d.Type == nameof(TokenEntityBase.UserId)).Value;
                principal.AddIdentity(new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, userId)
                }));
            }
            if (options.Cache)
            {
                var clientType = principal.Claims.FirstOrDefault(d => d.Type == nameof(TokenEntityBase.ClientType)).Value;
                if (userId == null)
                {
                    userId = principal.Claims.FirstOrDefault(d => d.Type == nameof(TokenEntityBase.UserId)).Value;
                }
                var tokenEntry = CacheEntryCollection.GetTokenEntry(jwtTokenType, clientType, userId, (int)options.AccessExpireMins * 60);

                var cacheToken = await _store.GetAsync <string>(tokenEntry);

                if (cacheToken.IsNullOrEmpty() || cacheToken != token)
                {
                    throw new RyeException("Token is error");
                }
            }
            return(principal);
        }
Exemplo n.º 3
0
        public virtual async Task DeleteTokenAsync(string userId, string clientType)
        {
            var tokenEntry = CacheEntryCollection.GetTokenEntry(JwtTokenType.AccessToken, clientType, userId);
            await _store.RemoveAsync(tokenEntry.Key);

            tokenEntry = CacheEntryCollection.GetTokenEntry(JwtTokenType.RefreshToken, clientType, userId);
            await _store.RemoveAsync(tokenEntry.Key);
        }