public async Task<MicrosoftUser> GetMeAsync(string token) { Guard.NotNullOrWhiteSpace(token, nameof(token)); var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var response = await client.GetAsync("https://apis.live.net/v5.0/me"); if (response.StatusCode != HttpStatusCode.OK) return null; var responseJsonString = await response.Content.ReadAsStringAsync(); var responseJson = JObject.Parse(responseJsonString); var user = new MicrosoftUser { Id = responseJson.Value<string>("id"), EmailAddress = responseJson.Value<JObject>("emails")?.Value<string>("preferred"), Locale = responseJson.Value<string>("locale")?.Replace("_", "-") }; if (string.IsNullOrWhiteSpace(user.EmailAddress) || string.IsNullOrWhiteSpace(user.Locale)) return null; return user; }
private void Button_Click_1(object sender, RoutedEventArgs e) { LoggedInUser = null; islogin = false; UserName.Content = UserUUID.Content = ""; LoginOut(); LoginChange(); }
private async Task Authenticate(string code) { try { TaskbarManager.SetProgressState(TaskbarProgressBarState.Indeterminate); var result = await OAuthFlower.MicrosoftCodeToAccessToken(code, CancelToken); var xbox_result = await XboxliveAuther.Authenticate(result, CancelToken); var mc_result = await McServices.Authenticate(xbox_result, CancelToken); var owner_result = await McServices.CheckHaveGameOwnership(mc_result, CancelToken); if (owner_result) { MicrosoftUser microsoftUser = await McServices.GetProfile(result, mc_result, CancelToken); LoggedInUser = microsoftUser; islogin = true; LoginChange(); } else { islogin = false; LoggedInUser = null; var res = await this.ShowMessageAsync(App.GetResourceString("String.OauthLogin.NoMinecraft"), App.GetResourceString("String.OauthLogin.NoMinecraft1"), MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() { AffirmativeButtonText = App.GetResourceString("String.OauthLogin.Back"), NegativeButtonText = App.GetResourceString("String.OauthLogin.Change"), DefaultButtonFocus = MessageDialogResult.Negative }); if (res == MessageDialogResult.Negative) { LoginOut(); } } } catch (Exception ex) { LoggedInUser = null; await this.ShowMessageAsync(App.GetResourceString("String.OauthLogin.LoginError"), ex.ToString()); } finally { TaskbarManager.SetProgressState(TaskbarProgressBarState.NoProgress); } }
public async Task <MicrosoftUser> GetProfile(MicrosoftToken ms_token, MinecraftToken mc_token, CancellationToken cancellation) { HttpRequestMessage request = new(HttpMethod.Get, ProfileUri); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", mc_token.AccessToken); var result = await HttpRequesterAPI.client.SendAsync(request, cancellation); result.EnsureSuccessStatusCode(); var respond_str = await result.Content.ReadAsStringAsync(); Console.WriteLine(respond_str); MicrosoftUser profile = JsonConvert.DeserializeObject <MicrosoftUser>(respond_str); profile.MicrosoftToken = ms_token; profile.MinecraftToken = mc_token; return(profile); }
private void MainForm_Load(object sender, EventArgs e) { MicrosoftUser.GetUserPicturePath(null, out var temp); winCircularPicturebox1.ImageLocation = temp; }
private void LoginWithMicrosoftAccount(object sender, RoutedEventArgs e) { User user = null; TABC_Users.SelectedItem = TAB_MicrosoftUser; Task t = new Task(() => { Thread.Sleep(600); try { user = MicrosoftUser.Login(); MainWindow.Current.Dispatcher.BeginInvoke((Action) delegate() { User.AddUser(user); }); } catch (OperationCanceledException) { MainWindow.Current.Dispatcher.BeginInvoke((Action) delegate() { TABC_Users.SelectedItem = TAB_SELUser; }); } catch (NoUserProfileException) { MainWindow.Current.Dispatcher.BeginInvoke((Action) delegate() { ShowMessageBox("登录失败", new TextBlock() { TextWrapping = TextWrapping.Wrap, Text = "此账号下没有Minecraft" }, new List <object> { "购买Minecraft" }, new List <RoutedEventHandler> { (object sX, RoutedEventArgs eX) => { Process.Start("https://www.minecraft.net/zh-hans/store/minecraft-java-edition"); mainDialog.IsOpen = false; } }); TABC_Users.SelectedItem = TAB_SELUser; }); } catch (Exception ex) { MainWindow.Current.Dispatcher.BeginInvoke((Action) delegate() { ShowMessageBox("登录失败", new TextBlock() { TextWrapping = TextWrapping.Wrap, Text = "未知错误" + ex.Message + ",请见启动器日志" }); Logger.LogError("There is an error when we login to the Microsof account :" + ex + "\n" + ex.Message + "\n" + ex.StackTrace); TABC_Users.SelectedItem = TAB_SELUser; }); } MainWindow.Current.Dispatcher.BeginInvoke((Action) delegate() { LoadUser(); TABC_Users.SelectedItem = TAB_SELUser; }); }); t.Start(); }