Example #1
0
        /// <summary>
        /// 创建令牌
        /// </summary>
        /// <param name="payload">负载</param>
        /// <param name="options">Jwt选项配置</param>
        public async Task <JsonWebToken> CreateAsync(IDictionary <string, string> payload, JwtOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.Secret))
            {
                throw new ArgumentNullException(nameof(_options.Secret),
                                                $@"{nameof(options.Secret)}为Null或空字符串。请在""appsettings.json""配置""{nameof(JwtOptions)}""节点及其子节点""{nameof(JwtOptions.Secret)}""");
            }
            var clientId   = payload.ContainsKey("clientId") ? payload["clientId"] : Guid.NewGuid().ToString();
            var clientType = payload.ContainsKey("clientType") ? payload["clientType"] : "admin";
            var userId     = GetUserId(payload);

            if (userId.IsEmpty())
            {
                throw new ArgumentException("不存在用户标识");
            }
            var claims = Helper.ToClaims(payload);

            // 生成刷新令牌
            var(refreshToken, refreshExpires) =
                Helper.CreateToken(_tokenHandler, claims, options, JsonWebTokenType.RefreshToken);
            var refreshTokenStr = refreshToken;
            await _tokenStore.SaveRefreshTokenAsync(new RefreshToken()
            {
                ClientId   = clientId,
                EndUtcTime = refreshExpires,
                Value      = refreshTokenStr
            });

            // 生成访问令牌
            var(token, accessExpires) =
                Helper.CreateToken(_tokenHandler, claims, _options, JsonWebTokenType.AccessToken);
            var accessToken = new JsonWebToken()
            {
                AccessToken           = token,
                AccessTokenUtcExpires = Conv.To <long>(accessExpires.ToJsGetTime()),
                RefreshToken          = refreshTokenStr,
                RefreshUtcExpires     = Conv.To <long>(refreshExpires.ToJsGetTime())
            };
            await _tokenStore.SaveTokenAsync(accessToken, accessExpires);

            // 绑定用户设备令牌
            await _tokenStore.BindUserDeviceTokenAsync(userId, clientType, new DeviceTokenBindInfo()
            {
                UserId     = userId,
                DeviceId   = clientId,
                DeviceType = clientType,
                Token      = accessToken,
            }, refreshExpires);

            // 存储payload
            await _tokenPayloadStore.SaveAsync(refreshToken, payload, refreshExpires);

            return(accessToken);
        }
        /// <summary>
        /// 创建令牌
        /// </summary>
        /// <param name="payload">负载</param>
        /// <param name="options">Jwt选项配置</param>
        public async Task <JsonWebToken> CreateAsync(IDictionary <string, string> payload, JwtOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.Secret))
            {
                throw new ArgumentNullException(nameof(_options.Secret),
                                                $@"{nameof(options.Secret)}为Null或空字符串。请在""appsettings.json""配置""{nameof(JwtOptions)}""节点及其子节点""{nameof(JwtOptions.Secret)}""");
            }
            var clientId = payload["clientId"] ?? Guid.NewGuid().ToString();
            var claims   = new List <Claim>();

            foreach (var key in payload.Keys)
            {
                var tempClaim = new Claim(key, payload[key]?.ToString());
                claims.Add(tempClaim);
            }

            // 生成刷新令牌
            var(refreshToken, refreshExpires) = CreateToken(claims, options, JsonWebTokenType.RefreshToken);
            var refreshTokenStr = refreshToken;
            await _tokenStore.SaveRefreshTokenAsync(new RefreshToken()
            {
                ClientId   = clientId,
                EndUtcTime = refreshExpires,
                Value      = refreshTokenStr
            });

            // 生成访问令牌
            var(token, accessExpires) = CreateToken(claims, _options, JsonWebTokenType.AccessToken);
            var accessToken = new JsonWebToken()
            {
                AccessToken           = token,
                AccessTokenUtcExpires = Conv.To <long>(accessExpires.ToJsGetTime()),
                RefreshToken          = refreshTokenStr,
                RefreshUtcExpires     = Conv.To <long>(refreshExpires.ToJsGetTime())
            };
            await _tokenStore.SaveTokenAsync(accessToken, accessExpires);

            return(accessToken);
        }