Example #1
0
        /// <summary>
        /// 验证用户的令牌openId
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="openId">用户的令牌</param>
        /// <param name="useCaching">采用缓存</param>
        /// <param name="useDataBase">采用数据库</param>
        /// <param name="useUserCenterHost">采用用户中心接口</param>
        /// <returns>验证通过</returns>
        public static bool ValidateOpenId(string userId, string openId, string cachingSystemCode = null, bool useCaching = true, bool useDataBase = false, bool useUserCenterHost = false)
        {
            bool result = false;

            if (string.IsNullOrEmpty(cachingSystemCode))
            {
                cachingSystemCode = string.Empty;
            }

            // 2016-03-14 吉日嘎拉、PDA系统的单独处理、其他的都认为是一样的。
            if (!cachingSystemCode.Equals("PDA"))
            {
                cachingSystemCode = string.Empty;
            }

            if (!string.IsNullOrWhiteSpace(openId))
            {
                // 使用缓存进行验证、效率高,减少数据库的I/O压力。
                if (useCaching)
                {
                    string key = string.Empty;
                    // 2015-11-20 吉日嘎拉 为了编译通过进行改进
                    using (var redisClient = PooledRedisHelper.GetReadOnlyClient())
                    {
                        if (string.IsNullOrEmpty(cachingSystemCode))
                        {
                            key = "openId:" + openId;
                        }
                        else
                        {
                            key = "openId:" + cachingSystemCode + ":" + openId;
                        }
                        result = redisClient.ContainsKey(key);
                    }
                }

                // 用数据库的方式进行验证
                if (!result && useDataBase)
                {
                    BaseUserLogOnManager userLogOnManager = new BaseUserLogOnManager();
                    result = userLogOnManager.ValidateOpenId(userId, openId, cachingSystemCode);
                    if (result)
                    {
                        // 提高缓存效率、若读取到了,写入到缓存里去
                        if (!string.IsNullOrWhiteSpace(userId) && useCaching)
                        {
                            SetUserOpenId(userId, openId, cachingSystemCode);
                        }
                        result = true;
                    }
                }

                // 不能访问数据库时、通过远程用户中心服务进行验证OpenId、通过服务方式进行验证
                if (!result && useUserCenterHost)
                {
                    string              url        = BaseSystemInfo.UserCenterHost + "/UserCenterV42/LogOnService.ashx";
                    WebClient           webClient  = new WebClient();
                    NameValueCollection postValues = new NameValueCollection();
                    if (!string.IsNullOrEmpty(cachingSystemCode))
                    {
                        postValues.Add("systemCode", cachingSystemCode);
                    }
                    postValues.Add("ipAddress", Utilities.GetIPAddress());
                    postValues.Add("securityKey", BaseSystemInfo.SecurityKey);
                    postValues.Add("function", "ValidateOpenId");
                    postValues.Add("userId", userId);
                    postValues.Add("openId", openId);
                    // 向服务器发送POST数据
                    byte[] responseArray = webClient.UploadValues(url, postValues);
                    string response      = Encoding.UTF8.GetString(responseArray);
                    if (!string.IsNullOrEmpty(response))
                    {
                        result = response.Equals(true.ToString(), StringComparison.InvariantCultureIgnoreCase);
                    }
                }
            }

            return(result);
        }