Пример #1
0
        /// <summary>
        /// Opens a login dialog to allow the user to login.
        /// </summary>
        /// 
        private void DoLogin(LoginCompleteHandler done, string message = "",
            bool canplayOffline = false)
        {
            string username = "";
            string password = "";
            ReadUserInfo(out username, out password);

            LoginDialog loginDlg = new LoginDialog(this, message, canplayOffline);
            if (!string.IsNullOrEmpty(username))
            {
                loginDlg.RememberUsername = true;
                loginDlg.Username = username;
            }
            if (!string.IsNullOrEmpty(password))
            {
                loginDlg.RememberPassword = true;
                loginDlg.Password = password;
            }
            loginDlg.Response += (object o, ResponseArgs args) =>
            {
                if (args.ResponseId == ResponseType.Ok)
                {
                    string parameters = string.Format(
                        "user={0}&password={1}&version=13",
                        Uri.EscapeDataString(loginDlg.Username),
                        Uri.EscapeDataString(loginDlg.Password), 13);

                    // Start a new thread and post the login info to login.minecraft.net
                    Thread loginThread = new Thread(
                        () =>
                        {
                            WriteUserInfo((loginDlg.RememberUsername ? loginDlg.Username : ""),
                                              (loginDlg.RememberPassword ? loginDlg.Password : ""));

                            string reply = "";
                            bool postFailed = false;
                            try
                            {
                                reply = AppUtils.ExecutePost("https://login.minecraft.net/",
                                        parameters);
                            }
                            catch (System.Net.WebException e)
                            {
                                postFailed = true;
                                reply = e.Message;
                            }

                            // If the login failed
                            if (!reply.Contains(":") || postFailed)
                            {
                                // Translate the error message to a more user friendly wording
                                string errorMessage = reply;
                                switch (reply.ToLower())
                                {
                                case "bad login":
                                    errorMessage = "Invalid username or password.";
                                    break;
                                case "old version":
                                    errorMessage = "Please update.";
                                    break;
                                default:
                                    errorMessage = "Login failed: " + reply;
                                    break;
                                }

                                // Error
                                Application.Invoke((sender, e) => DoLogin(done, errorMessage));
                            }

                                // If the login succeeded
                            else
                            {
                                string[] responseValues = reply.Split(':');

                                // The response must have 4 values or it's invalid
                                if (responseValues.Length != 4)
                                {
                                    // Error
                                    Application.Invoke(
                                            (sender, e) =>
                                            DoLogin(done, "Got an invalid response from server"));
                                }
                                // Now we can finally return our login info.
                                else
                                {
                                    LoginInfo info = new LoginInfo(responseValues,
                                                                       loginDlg.ForceUpdate);
                                    done(info);
                                }
                            }
                        });
                    loginThread.Start();
                }
                else if (args.ResponseId == ResponseType.Reject)
                {
                    // Play offline
                    done(new LoginInfo(null, false, false));
                }
                else
                {
                    // Login cancelled
                    done(new LoginInfo());
                }
                loginDlg.Destroy();
            };
            loginDlg.Run();
        }
Пример #2
0
        /// <summary>
        /// Opens a login dialog to allow the user to login.
        /// </summary>
        private void DoLogin(LoginCompleteHandler done, string message = "")
        {
            LoginDialog loginDlg = new LoginDialog(this, message);
            loginDlg.Response += (object o, ResponseArgs args) =>
            {
                if (args.ResponseId == ResponseType.Ok)
                {
                    Console.WriteLine("OK Clicked");
                    string parameters = Uri.EscapeUriString(string.Format(
                        "user={0}&password={1}&version={2}",
                        loginDlg.Username, loginDlg.Password, 13));

                    // Start a new thread and post the login info to login.minecraft.net
                    Thread loginThread = new Thread(
                        () =>
                    {
                        string reply = AppUtils.ExecutePost("https://login.minecraft.net",
                                                            parameters);

                        // If the login failed
                        if (!reply.Contains(":"))
                        {
                            // Translate the error message to a more user friendly wording
                            string errorMessage = reply;
                            switch (reply.ToLower())
                            {
                            case "bad login":
                                errorMessage = "Invalid username or password";
                                break;
                            // TODO add more error messages
                            default:
                                errorMessage = "Login failed: " + reply;
                                break;
                            }

                            // Error
                            Application.Invoke((sender, e) => DoLogin(done, errorMessage));
                        }

                        // If the login succeeded
                        else
                        {
                            string[] responseValues = reply.Split(':');

                            // The response must have 4 values or it's invalid
                            if (responseValues.Length != 4)
                            {
                                // Error
                                Application.Invoke(
                                    (sender, e) =>
                                    DoLogin(done, "Got an invalid response from server"));
                            }
                            // Now we can finally return our login info.
                            else
                            {
                                LoginInfo info = new LoginInfo(responseValues,
                                                               loginDlg.ForceUpdate);
                                done(info);
                            }
                        }
                    });
                    loginThread.Start();
                }
                else
                {
                    // Login cancelled
                    done(new LoginInfo());
                }
                loginDlg.Destroy();
            };
            loginDlg.Run();
        }