private static LoginResult MicrosoftLogin(XboxLive.UserLoginResponse msaResponse, out SessionToken session) { session = new SessionToken() { ClientID = Guid.NewGuid().ToString().Replace("-", "") }; var ms = new XboxLive(); var mc = new MinecraftWithXbox(); try { var xblResponse = ms.XblAuthenticate(msaResponse); var xsts = ms.XSTSAuthenticate(xblResponse); // Might throw even password correct string accessToken = mc.LoginWithXbox(xsts.UserHash, xsts.Token); bool hasGame = mc.UserHasGame(accessToken); if (hasGame) { var profile = mc.GetUserProfile(accessToken); session.PlayerName = profile.UserName; session.PlayerID = profile.UUID; session.ID = accessToken; return(LoginResult.Success); } else { return(LoginResult.NotPremium); } } catch (Exception e) { ConsoleIO.WriteLineFormatted("§cMicrosoft authenticate failed: " + e.Message); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§c" + e.StackTrace); } return(LoginResult.WrongPassword); // Might not always be wrong password } }
/// <summary> /// Sign-in to Microsoft Account by asking user to open sign-in page using browser. /// </summary> /// <remarks> /// The downside is this require user to copy and paste lengthy content from and to console. /// Sign-in page: 218 chars /// Response URL: around 1500 chars /// </remarks> /// <param name="session"></param> /// <returns></returns> public static LoginResult MicrosoftBrowserLogin(out SessionToken session) { var ms = new XboxLive(); string[] askOpenLink = { "Copy the following link to your browser and login to your Microsoft Account", ">>>>>>>>>>>>>>>>>>>>>>", "", ms.SignInUrl, "", "<<<<<<<<<<<<<<<<<<<<<<", "NOTICE: Once successfully logged in, you will see a blank page in your web browser.", "Copy the contents of your browser's address bar and paste it below to complete the login process.", }; ConsoleIO.WriteLine(string.Join("\n", askOpenLink)); string[] parts = { }; while (true) { string link = ConsoleIO.ReadLine(); if (string.IsNullOrEmpty(link)) { session = new SessionToken(); return(LoginResult.UserCancel); } parts = link.Split('#'); if (parts.Length < 2) { ConsoleIO.WriteLine("Invalid link. Please try again."); continue; } else { break; } } string hash = parts[1]; var dict = Request.ParseQueryString(hash); var msaResponse = new XboxLive.UserLoginResponse() { AccessToken = dict["access_token"], RefreshToken = dict["refresh_token"], ExpiresIn = int.Parse(dict["expires_in"]) }; try { return(MicrosoftLogin(msaResponse, out session)); } catch (Exception e) { session = new SessionToken() { ClientID = Guid.NewGuid().ToString().Replace("-", "") }; ConsoleIO.WriteLineFormatted("§cMicrosoft authenticate failed: " + e.Message); if (Settings.DebugMessages) { ConsoleIO.WriteLineFormatted("§c" + e.StackTrace); } return(LoginResult.WrongPassword); // Might not always be wrong password } }