Пример #1
0
        /// <summary>
        /// 根据第三方OpenId查询绑定信息
        /// </summary>
        public async Task <ExternalUserEntity> GetExternalUser(int clientId, string providerKey, bool rebuild = false)
        {
            var key = GetExternalUserRedisKey(clientId, providerKey);
            ExternalUserEntity externalUserEntity;

            if (!rebuild)
            {
                externalUserEntity = await _redisCache.GetAsync <ExternalUserEntity>(key);

                if (externalUserEntity != null)
                {
                    return(externalUserEntity);
                }
            }

            externalUserEntity = await _externalUserRepository.TableNoTracking.FirstOrDefaultAsync(
                c => c.ProviderKey == providerKey && c.ClientId == clientId);

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

            await _redisCache.AddAsync(key, externalUserEntity, TimeSpan.FromDays(15));

            return(externalUserEntity);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task <ActionObjectResult <bool> > ValidSmsAsync(string phone, string code)
        {
            var uniqueKey = GetSmsKey(phone);
            var smsCache  = await _redisCache.GetAsync <SmsCache>(uniqueKey);

            if (smsCache == null)
            {
                return(ActionObject.Ok(false, -1, "未发送验证码或验证码已超时"));
            }

            if (smsCache.ValidateCount >= 3)  //0 1  2
            {
                return(ActionObject.Ok(false, -1, "输入错误的次数超过3次,请重新获取验证码"));
            }
            if (code == smsCache.Code)
            {
                await _redisCache.KeyDeleteAsync(uniqueKey);

                return(ActionObject.Ok(true));
            }

            var timeSpan = smsCache.StartTime.AddMinutes(smsCache.ExpiresMinute) - DateTime.Now;

            if (timeSpan.TotalSeconds <= 0)
            {
                return(ActionObject.Ok(false, -1, "未发送验证码或验证码已超时"));
            }
            smsCache.ValidateCount += 1;   //更新验证次数,不延长时间
            await _redisCache.AddAsync(uniqueKey, smsCache, timeSpan);

            return(ActionObject.Ok(false, -1, "输入验证码不正确"));
        }
        public async Task <ClientEntity> GetClientByIdAsync(int clientId)
        {
            var key          = GetCacheKey(clientId);
            var clientEntity = await _redisCache.GetAsync <ClientEntity>(key);

            if (clientEntity != null)
            {
                return(clientEntity.ClientId == -1 ? null : clientEntity);
            }

            var expiry = TimeSpan.FromSeconds(5);
            var wait   = TimeSpan.FromSeconds(5);
            var retry  = TimeSpan.FromSeconds(1);

            using (var redLock = await _redisCache.CreateLockAsync(key, expiry, wait, retry))
            {
                if (!redLock.IsAcquired)
                {
                    return(null);
                }

                clientEntity = await _redisCache.GetAsync <ClientEntity>(key);

                if (clientEntity?.ClientId != -1)
                {
                    return(clientEntity);
                }

                clientEntity = await _clientRepository.TableNoTracking.FirstOrDefaultAsync(c => c.ClientId == clientId);

                if (clientEntity != null)
                {
                    await _redisCache.AddAsync(key, clientEntity);
                }
                else
                {
                    //防止不存在的clientId频繁访问数据库
                    await _redisCache.AddAsync(key, new ClientEntity { ClientId = -1 }, expiry : TimeSpan.FromMinutes(15));
                }
            }

            return(clientEntity);
        }
Пример #4
0
        private async Task <(string, JwtToken)> GetCacheToken()
        {
            var key = $"client_credentials:{_appSettings.ClientId}";

            var tokenResult = await _redisCache.GetAsync <JwtToken>(key);

            if (!string.IsNullOrWhiteSpace(tokenResult?.AccessToken))
            {
                return("", tokenResult);
            }

            var(tokenerror, result) = await _authorizeTokenClient.GetToken();

            if (!string.IsNullOrEmpty(tokenerror))
            {
                return(tokenerror, null);
            }

            await _redisCache.AddAsync(key, result, TimeSpan.FromSeconds(int.Parse(result.ExpiresIn) - 300));

            return("", result);
        }