Ejemplo n.º 1
0
        /// <summary>
        /// 判断登录状态
        /// </summary>
        /// <param name="token">登录票据</param>
        /// <param name="facilityMark">登录设备</param>
        /// <param name="loginMark">登录设备标识</param>
        /// <returns>-1未登录,1登录成功,0登录过期,-2账号被顶</returns>
        public async Task <OperatorResult> IsOnLine(string token, string facilityMark, string loginMark)
        {
            OperatorResult operatorResult = new OperatorResult();

            operatorResult.stateCode = -1; // -1未登录,1登录成功,0登录过期
            try
            {
                if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(loginMark))
                {
                    return(operatorResult);
                }
                OperatorModel operatorInfo = await CacheHelper.Get <OperatorModel>(cacheKeyOperator + loginMark);

                if (operatorInfo != null)
                {
                    if (operatorInfo.LoginToken == token || LoginProvider == Define.PROVIDER_WEBAPI)
                    {
                        TimeSpan span = (TimeSpan)(DateTime.Now - operatorInfo.LoginTime);
                        //超时
                        if (span.TotalHours >= 12)// 登录操作过12小时移除
                        {
                            operatorResult.stateCode = 0;
                            Dictionary <string, string> tokenMarkList = await CacheHelper.Get <Dictionary <string, string> >(cacheKeyToken + operatorInfo.UserId);

                            tokenMarkList.Remove(loginMark);
                            await CacheHelper.Set(cacheKeyToken + operatorInfo.UserId, tokenMarkList);

                            await CacheHelper.Remove(cacheKeyOperator + loginMark);
                        }
                        //账号被顶(排除admin)
                        else if (!LoginMultiple && !operatorInfo.IsSystem && token != await CacheHelper.Get <string>(cacheKeyOperator + facilityMark + operatorInfo.UserId))
                        {
                            operatorResult.stateCode = -2;
                            Dictionary <string, string> tokenMarkList = await CacheHelper.Get <Dictionary <string, string> >(cacheKeyToken + operatorInfo.UserId);

                            tokenMarkList.Remove(loginMark);
                            await CacheHelper.Set(cacheKeyToken + operatorInfo.UserId, tokenMarkList);

                            await CacheHelper.Remove(cacheKeyOperator + loginMark);
                        }
                        else if (LoginProvider == Define.PROVIDER_WEBAPI && !operatorInfo.IsSystem && operatorInfo.LoginToken != await CacheHelper.Get <string>(cacheKeyOperator + facilityMark + operatorInfo.UserId))
                        {
                            operatorResult.stateCode = -2;
                            Dictionary <string, string> tokenMarkList = await CacheHelper.Get <Dictionary <string, string> >(cacheKeyToken + operatorInfo.UserId);

                            tokenMarkList.Remove(loginMark);
                            await CacheHelper.Set(cacheKeyToken + operatorInfo.UserId, tokenMarkList);

                            await CacheHelper.Remove(cacheKeyOperator + loginMark);
                        }
                        else
                        {
                            operatorResult.userInfo  = operatorInfo;
                            operatorResult.stateCode = 1;
                        }
                    }
                }
                return(operatorResult);
            }
            catch (Exception)
            {
                return(operatorResult);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 登录者信息添加到缓存中
        /// </summary>
        /// <param name="userEntity">用户</param>
        /// <param name="loginMark">设备标识uid</param>
        /// <param name="facilityMark">设备类型</param>
        /// <param name="cookie">是否保存cookie,默认是</param>
        /// <returns></returns>
        public async Task <string> AddLoginUser(OperatorModel operatorModel, string loginMark, string facilityMark, bool cookie = true)
        {
            string token = Guid.NewGuid().ToString();

            try
            {
                // 填写登录信息
                operatorModel.LoginToken = token;
                //cookid登录信息更新
                if (cookie)
                {
                    string cookieMark = GetProvider(LoginUserMarkKey);
                    if (string.IsNullOrEmpty(cookieMark))
                    {
                        operatorModel.loginMark = Guid.NewGuid().ToString();
                        SetProvider(LoginUserMarkKey, operatorModel.loginMark);
                    }
                    else
                    {
                        operatorModel.loginMark = cookieMark;
                    }
                    SetProvider(LoginUserToken, token);
                }
                else
                {
                    operatorModel.loginMark = loginMark;
                }
                //redis 登录token列表更新
                Dictionary <string, string> tokenMarkList = await CacheHelper.Get <Dictionary <string, string> >(cacheKeyToken + operatorModel.UserId);

                if (tokenMarkList == null)// 此账号第一次登录
                {
                    tokenMarkList = new Dictionary <string, string>();
                    tokenMarkList.Add(operatorModel.loginMark, token);
                }
                else
                {
                    if (tokenMarkList.ContainsKey(operatorModel.loginMark))
                    {
                        tokenMarkList[operatorModel.loginMark] = token;
                    }
                    else
                    {
                        tokenMarkList.Add(operatorModel.loginMark, token);
                    }
                }

                await CacheHelper.Set(cacheKeyToken + operatorModel.UserId, tokenMarkList);

                await CacheHelper.Set(cacheKeyOperator + operatorModel.loginMark, operatorModel, 12);

                await CacheHelper.Remove(cacheKeyOperator + facilityMark + operatorModel.UserId);

                await CacheHelper.Set(cacheKeyOperator + facilityMark + operatorModel.UserId, token, 12);

                return(token);
            }
            catch (Exception)
            {
                throw;
            }
        }