Ejemplo n.º 1
0
        private void DoLogin()
        {
            if (loginManager.AllowLogin())
            {
                try
                {
                    if (keepAliveTimer != null)
                    {
                        keepAliveTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
                    }

                    session = -1;
                    // Log in now
                    // First, we need to get some info from the server
                    string url      = "http://" + host + "/RPC2_Login";
                    string request  = "{\"method\":\"global.login\",\"params\":{\"userName\":\"" + user + "\",\"password\":\"\",\"clientType\":\"Web3.0\"},\"id\":10000}";
                    string response = HttpPost(url, request);

                    var r1 = JSON.ParseJson(response);
                    if (r1.error.code == 401)
                    {
                        // Unauthorized error; we expect this
                        session = r1.session;
                        string encryptionType = getJsonStringValue(response, "encryption");
                        string pwtoken;
                        if (encryptionType == "Basic")
                        {
                            pwtoken = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(user + ":" + pass));
                        }
                        else if (encryptionType == "Default")
                        {
                            string realm  = getJsonStringValue(response, "realm");
                            string random = getJsonStringValue(response, "random");
                            pwtoken  = Hash.GetMD5Hex(user + ":" + realm + ":" + pass).ToUpper();
                            pwtoken  = Hash.GetMD5Hex(user + ":" + random + ":" + pwtoken).ToUpper();
                            pwtoken += pass;                             // Hey, I didn't invent this "encryption" type. LOL
                        }
                        else
                        {
                            pwtoken = pass;
                        }

                        // Finally, we can try to log in.
                        request  = "{\"method\":\"global.login\",\"session\":" + session + ",\"params\":{\"userName\":\"" + user + "\",\"password\":\"" + pwtoken + "\",\"clientType\":\"Web3.0\"},\"id\":10000}";
                        response = HttpPost(url, request);
                    }
                    else
                    {
                        Logger.Debug("Login error code not 401. Request Info Follows" + Environment.NewLine + "URL: " + url + Environment.NewLine + "Request: " + request + Environment.NewLine + "Response: " + response);
                    }

                    keepAliveTimer = new System.Threading.Timer(new TimerCallback(timerTick), this, 1200, 1200);
                }
                catch (Exception ex)
                {
                    Logger.Debug(ex);
                }
            }
        }