コード例 #1
0
        public IdentityInfo Authenticate(string refUserCode)
        {
            var up = GetUserPermission(refUserCode);

            if (up.Permissions.Count == 0)
            {
                throw new AuthException("未授权用户");
            }
            var tokenInfo = new TokenInfo {
                AccessTime = DateTime.Now, RefUserCode = refUserCode
            };
            var          token        = TokenHelper.EncryptToken(tokenInfo);
            IdentityInfo identityInfo = CreateIdentityInfo();

            this._identityInfoType   = identityInfo.GetType();
            identityInfo.RefUserCode = refUserCode;
            identityInfo.Token       = token;
            identityInfo.ExpireTime  = DateTime.Now.AddMinutes(_identity_distribute_expire_minutes);
            bool flag = cacheManager.Set(_cache_distribute_token_key, identityInfo, _identity_distribute_expire_minutes, token);

            if (!flag)
            {
                throw new AuthException("令牌缓存失败");
            }
            cacheManager.Set(_cache_distribute_token_touch_key, 1, _identity_distribute_expire_minutes - 5, token);
            return(identityInfo);
        }
コード例 #2
0
        public bool IsAuthenticate(string token, out IdentityInfo identityInfo)
        {
            identityInfo = null;
            string cacheString;

            if (cacheManager.RawIsSet(_cache_distribute_token_key, out cacheString, token))
            {
                if (this._identityInfoType == null)
                {
                    this._identityInfoType = CreateIdentityInfo().GetType();
                }
                identityInfo = (IdentityInfo)JsonSerializerManager.JsonDeserialize(cacheString, this._identityInfoType);
                string touchValue;
                if (!cacheManager.RawIsSet(_cache_distribute_token_touch_key, out touchValue, token))
                {
                    cacheManager.RawSet(_cache_distribute_token_key, cacheString, _identity_distribute_expire_minutes, token);
                    cacheManager.Set(_cache_distribute_token_touch_key, 1, _identity_distribute_expire_minutes - 5, token);
                }
                return(true);
            }
            return(false);
        }