예제 #1
0
        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns></returns>
        public static async Task <string> RefreshToken(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            FormUrlEncodedCollection parameters;

            parameters = new FormUrlEncodedCollection {
                { "access_key", user.LoginData["access_key"] },
                { "refresh_token", user.LoginData["refresh_token"] },
                { "ts", ApiUtils.GetTimeStamp().ToString() }
            };
            parameters.AddRange(user.LoginData["cookie"].Split(';').Select(item => {
                string[] pair;

                pair = item.Split('=');
                return(new KeyValuePair <string, string>(pair[0], pair[1]));
            }));
            parameters.AddRange(General);
            parameters.SortAndSign();
            using (HttpResponseMessage response = await user.Client.SendAsync(HttpMethod.Post, OAUTH2_REFRESH_TOKEN_URL, parameters, user.AppHeaders))
                return(await response.Content.ReadAsStringAsync());
        }
예제 #2
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="jsonKey">Key</param>
        /// <param name="captcha">验证码</param>
        /// <returns></returns>
        public static async Task <string> Login(User user, string jsonKey, string captcha)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (string.IsNullOrEmpty(jsonKey))
            {
                throw new ArgumentNullException(nameof(jsonKey));
            }

            JToken                   loginKey;
            string                   rsaKey;
            RSAParameters            rsaParameters;
            FormUrlEncodedCollection parameters;

            loginKey      = JObject.Parse(jsonKey)["data"];
            rsaKey        = loginKey["key"].ToString();
            rsaKey        = rsaKey.Replace("\n", string.Empty).Substring(26, rsaKey.Length - 56);
            rsaParameters = ApiUtils.ParsePublicKey(rsaKey);
            parameters    = new FormUrlEncodedCollection {
                { "username", user.Account },
                { "password", ApiUtils.RsaEncrypt(loginKey["hash"] + user.Password, rsaParameters) },
                { "captcha", captcha ?? string.Empty }
            };
            parameters.AddRange(General);
            parameters.SortAndSign();
            using (HttpResponseMessage response = await user.Client.SendAsync(HttpMethod.Post, OAUTH2_LOGIN_URL, parameters, null))
                return(await response.Content.ReadAsStringAsync());
        }
예제 #3
0
        /// <summary>
        /// 获取Key
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns></returns>
        public static async Task <string> GetKey(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            FormUrlEncodedCollection parameters;

            parameters = new FormUrlEncodedCollection {
                { "appkey", General["appkey"] }
            };
            parameters.SortAndSign();
            using (HttpResponseMessage response = await user.Client.SendAsync(HttpMethod.Post, OAUTH2_GETKEY_URL, parameters, null))
                return(await response.Content.ReadAsStringAsync());
        }
예제 #4
0
 public static void SortAndSign(this FormUrlEncodedCollection parameters)
 {
     parameters.Sort((x, y) => x.Key[0] - y.Key[0]);
     parameters.Add("sign", ComputeSign(parameters.FormToString()));
 }