Beispiel #1
0
        /// <summary>
        /// 根据用户登录账号获取Account缓存中的用户ID
        /// </summary>
        /// <param name="account">登录账号(账号、手机号、E-mail)</param>
        /// <returns>用户ID</returns>
        public static string getUserId(string account)
        {
            var key    = "ID:" + account;
            var userId = RedisHelper.stringGet(key);

            if (!string.IsNullOrEmpty(userId))
            {
                return(userId);
            }

            mutex.WaitOne();
            try
            {
                userId = RedisHelper.stringGet(account);
                if (!string.IsNullOrEmpty(userId))
                {
                    mutex.ReleaseMutex();
                    return(userId);
                }

                var user = getUser(account);
                if (user == null)
                {
                    mutex.ReleaseMutex();
                    return(null);
                }

                // 缓存用户ID到Redis
                userId = user.id;
                key    = "ID:" + user.account;
                RedisHelper.stringSet(key, userId);

                if (!string.IsNullOrEmpty(user.mobile))
                {
                    key = "ID:" + user.mobile;
                    RedisHelper.stringSet(key, userId);
                }

                if (!string.IsNullOrEmpty(user.email))
                {
                    key = "ID:" + user.email;
                    RedisHelper.stringSet(key, userId);
                }

                var token = new TokenManage(user);
                setUserCache(token);
                mutex.ReleaseMutex();

                return(userId);
            }
            catch (Exception ex)
            {
                new Thread(() => Logger.write("200100", ex.StackTrace, "Core", "getUserId")).Start();
                mutex.ReleaseMutex();

                return(null);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 保存用户数据到缓存
        /// </summary>
        /// <param name="token">Token</param>
        public static void setUserCache(TokenManage token)
        {
            if (!token.isChanged())
            {
                return;
            }

            var key = $"User:{token.userId}";

            RedisHelper.stringSet(key, token);
        }
Beispiel #3
0
        /// <summary>
        /// 生成Code,缓存后返回
        /// </summary>
        /// <param name="token">Token</param>
        /// <param name="account">登录账号</param>
        /// <param name="type">登录类型(0:密码登录、1:验证码登录)</param>
        /// <returns>Code</returns>
        public static string generateCode(TokenManage token, string account, int type)
        {
            string key;
            var    life = 5;

            switch (type)
            {
            case 0:
                key = Util.hash(account + token.password);
                break;

            case 1:
                // 生成短信验证码(5分钟内有效)并发送
                var mobile = token.mobile;
                if (string.IsNullOrEmpty(mobile))
                {
                    return(null);
                }

                life = 60 * 5;
                var smsCode = generateSmsCode(4, mobile, 5, 4);
                key = Util.hash(mobile + Util.hash(smsCode));
                break;

            default:
                // Invalid type! You guess, you guess, you guess. (≧∇≦)
                key = Util.newId("N");
                break;
            }

            var code      = Util.newId("N");
            var signature = Util.hash(key + code);

            // 缓存签名-Code,以及Code-用户ID.
            RedisHelper.stringSet(signature, code, TimeSpan.FromSeconds(life));
            RedisHelper.stringSet(code, token.userId, TimeSpan.FromSeconds(life));

            return(code);
        }
Beispiel #4
0
        /// <summary>
        /// 会话合法性验证
        /// </summary>
        /// <param name="key">操作权限代码,默认为空,即不进行鉴权</param>
        /// <param name="id">用户ID</param>
        /// <returns>bool 身份是否通过验证</returns>
        protected bool verify(string key = null, string id = null)
        {
            var verify = new Verify();

            tokenId = verify.tokenId;
            manage  = verify.manage;
            if (manage == null)
            {
                return(false);
            }

            appId      = manage.getAppId();
            tenantId   = manage.getTenantId();
            tenantCode = manage.getTenantCode();
            tenantName = manage.getTenantName();
            deptId     = manage.getDeptId();
            deptCode   = manage.getDeptCode();
            deptName   = manage.getDeptName();
            userId     = manage.userId;
            userName   = manage.userName;
            result     = verify.compare(userId == id ? null : key);

            return(result.successful);
        }