Exemplo n.º 1
0
        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns></returns>
        public static async Task <bool> RefreshToken(this User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            string  json;
            JObject result;

            try {
                json = await LoginApi.RefreshTokenAsync(user);

                result = JObject.Parse(json);
            }
            catch (Exception ex) {
                throw new ApiException(ex);
            }
            if ((int)result["code"] == 0 && result["data"]["token_info"]["mid"] != null)
            {
                user.LogInfo("Token刷新成功");
                UpdateLoginData(user, result);
                OnLoginDataUpdated(new LoginDataUpdatedEventArgs(user));
                return(true);
            }
            else
            {
                user.LogError("Token刷新失败");
                user.LogError($"错误信息:{Utils.FormatJson(json)}");
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns></returns>
        public static async Task <bool> RefreshToken(this User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            string     json;
            ResultInfo result;

            try
            {
                json = await LoginApi.RefreshTokenAsync(user);

                if (string.IsNullOrEmpty(json))
                {
                    throw new ApiException(new Exception("登录失败,没有返回的数据。"));
                }
                if (json.ToLower().Contains("doctype html"))
                {
                    throw new ApiException(new Exception("登录失败,刷新Token失败。"));
                }

                result = JsonHelper.DeserializeJsonToObject <ResultInfo>(json);
            }
            catch (Exception ex)
            {
                throw new ApiException(ex);
            }
            if (result.Code == 0 && !string.IsNullOrEmpty(result.Data.TokenInfo.Mid))
            {
                user.LogInfo("Token刷新成功");
                UpdateLoginData(user, result);
                OnLoginDataUpdated(new LoginDataUpdatedEventArgs(user));
                return(true);
            }
            else
            {
                user.LogError("Token刷新失败");
                user.LogError($"错误信息:{Utils.FormatJson(json)}");
                return(false);
            }
        }