public void Login() { LoginDialog login = new LoginDialog (); var saved = GetSavedCredentials(); try { if (saved != null) { login.Username = saved.AutoLoginUser; var savedCreds = saved.Credentials.Where(x => x.UserName == saved.AutoLoginUser).FirstOrDefault(); if (savedCreds != null) { login.Password = savedCreds.Password; login.RememberCredentials = true; } } while (true) { int response = login.Run (); if (response == (int)Gtk.ResponseType.Ok) { var auth = new MinecraftAuthentication (); string message = "Invalid login!"; try { Session = auth.Login (login.Username, login.Password); } catch (Exception e) { Session = null; message = e.Message; } if (Session == null || Session.AccessToken == null) { DedicatedLauncher.MessageBox (message); continue; } break; } else if (response == (int)Gtk.ResponseType.Cancel) { throw new CancelException(); } } saved = new SavedCredentials() { AutoLoginUser = login.Username, Credentials = new List<SavedCredential>() }; if (login.RememberCredentials) { saved.Credentials = new List<SavedCredential>() { new SavedCredential() { UserName = login.Username, Password = login.Password } }; } Directory.CreateDirectory(System.IO.Path.GetDirectoryName(SavedCredentialsFile)); using (StreamWriter sw = new StreamWriter(SavedCredentialsFile)) sw.Write(saved.ToJson()); login.Hide (); login.Destroy(); usernameLabel.Markup = string.Format ("Playing as <b>{0}</b>", Session.Username); } finally { login.Destroy(); } }
public void Login() { LoginDialog login = new LoginDialog (); SavedCredentials saved = null; string credsFileName = Path.Combine(Instance.GameFolder, "credentials.json"); if (File.Exists(credsFileName)) { using (var sr = new StreamReader(credsFileName)) saved = SavedCredentials.Parse(sr.ReadToEnd()); } if (saved != null) { login.Username = saved.AutoLoginUser; var savedCreds = saved.Credentials.Where(x => x.UserName == saved.AutoLoginUser).FirstOrDefault(); if (savedCreds != null) { login.Password = savedCreds.Password; login.RememberCredentials = true; } } while (true) { int response = login.Run (); if (response == (int)Gtk.ResponseType.Ok) { var auth = new MinecraftAuthentication (); string message = "Invalid login!"; try { Session = auth.Login (login.Username, login.Password); } catch (Exception e) { Session = null; message = e.Message; } if (Session == null || Session.AccessToken == null) { MessageBox (message); continue; } break; } else if (response == (int)Gtk.ResponseType.Cancel) { Gtk.Application.Quit(); Environment.Exit(0); } } saved = new SavedCredentials() { AutoLoginUser = login.Username, Credentials = new List<SavedCredential>() }; if (login.RememberCredentials) { saved.Credentials = new List<SavedCredential>() { new SavedCredential() { UserName = login.Username, Password = login.Password } }; } Directory.CreateDirectory(Path.GetDirectoryName(credsFileName)); using (StreamWriter sw = new StreamWriter(credsFileName)) sw.Write(saved.ToJson()); login.Hide (); login.Destroy(); }