public async Task <ApiRequestResult> GetAsync()
        {
            var token     = HttpContext.Request.Headers["Authorization"].ToString();
            var key       = token.Replace("Bearer", "").Trim();
            var jsonRedis = await _cache.GetStringAsync(key);

            if (jsonRedis.IsNullOrEmpty())
            {
                return(ApiRequestResult.Error("登录已过期", HttpStatusCode.Unauthorized));
            }
            var jsonWebToken = JsonConvert.DeserializeObject <JsonWebToken>(jsonRedis);
            var accountUser  = await _identityServices.GetAsync(Guid.Parse(jsonWebToken.Id));

            var roles = new List <string>
            {
                "TEST"
            };
            var userInfo = new UserInfoDto
            {
                UserName = accountUser.UserName,
                Roles    = roles,
            };

            return(ApiRequestResult.Success(userInfo, ""));
        }
Beispiel #2
0
        /// <summary>
        /// 获取微信accesstoken, 优先从缓存获取,其次重新请求获取
        /// </summary>
        /// <returns></returns>
        public async Task <HttpRequestResult> GetAccessTokenAsync()
        {
            var accessToken = await _aSampleRedisCache.GetStringAsync(_wechatKey);

            if (!string.IsNullOrWhiteSpace(accessToken))
            {
                return(HttpRequestResult.Success(accessToken, ""));
            }
            var result = await RequestAccessTokenAsync();

            if (string.IsNullOrWhiteSpace(result.AccessToken))
            {
                return(HttpRequestResult.Error("AccessToken is null"));
            }
            await _aSampleRedisCache.SetStringAsync(_wechatKey, result.AccessToken, 120);

            return(HttpRequestResult.Success(result.AccessToken, ""));
        }