コード例 #1
0
        //获取rsa的加密密钥
        private static void _fetch_encrypt_key(out string hash, out string public_key)
        {
            var ns    = new NetStream();
            var param = new Parameters();

            param.Add("appkey", APPKEY);
            param.Add("build", "513000");
            param.Add("mobi_app", "android");
            param.Add("platform", "android");
            param.Add("ts", (long)util.ToUnixTimestamp(DateTime.Now));
            param.Add("sign", CalculateSign(param));

            ns.UserAgent = ANDROID_USER_AGENT;
            try
            {
                ns.HttpPost(_URL_AUTH_GET_KEY, param);
                ns.HttpPostClose();

                var response = ns.ReadResponseString();
                var json     = JsonConvert.DeserializeObject(response) as JObject;

                int code = json.Value <int>("code");
                if (code != 0)
                {
                    hash                = null;
                    public_key          = null;
                    _last_error_code    = code;
                    _last_error_message = json.Value <string>("message");
                }
                else
                {
                    hash       = json["data"].Value <string>("hash");
                    public_key = json["data"].Value <string>("key");
                }
            }
            catch (Exception ex)
            {
                Tracer.GlobalTracer.TraceError(ex.ToString());
                throw ex;
            }
            finally
            {
                ns.Close();
            }
        }
コード例 #2
0
        public static bool Login(string username, string password)
        {
            string hash, public_key;

            _fetch_encrypt_key(out hash, out public_key);

            var ns    = new NetStream();
            var param = new Parameters();

            param.Add("appkey", APPKEY);
            param.Add("build", "513000");
            param.Add("mobi_app", "android");
            param.Add("password", _encrypt_password(hash, public_key, password));
            param.Add("platform", "android");
            param.Add("ts", (long)util.ToUnixTimestamp(DateTime.Now));
            param.Add("username", username);
            param.Add("sign", CalculateSign(param));

            ns.UserAgent = ANDROID_USER_AGENT;
            try
            {
                ns.HttpPost(_URL_AUTH_LOGIN, param);
                ns.HttpPostClose();

                var response = ns.ReadResponseString();
                var json     = JsonConvert.DeserializeObject(response) as JObject;

                int code = json.Value <int>("code");
                if (code != 0)
                {
                    _last_error_code    = code;
                    _last_error_message = json.Value <string>("message");
                    return(false);
                }
                else
                {
                    _access_token = json["data"].Value <string>("access_token");
                    var ts = util.FromUnixTimestamp(json.Value <long>("ts"));
                    _expire_time   = ts.AddSeconds(json["data"].Value <long>("expires_in"));
                    _mid           = json["data"].Value <uint>("mid");
                    _refresh_token = json["data"].Value <string>("refresh_token");

                    //initialize other param
                    try
                    {
                        _fetch_auth_info();
                    }
                    catch { }
                    while (!_sso_auth())
                    {
                        Thread.Sleep(3000);
                    }
                    //NetStream.SaveCookie();
                    _save_token();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Tracer.GlobalTracer.TraceError(ex.ToString());
                throw ex;
            }
            finally
            {
                ns.Close();
            }
        }