World War I Windows 10 (84) and World War I 84
[Download source code]
Backwater world war I Windows 10 (84)-users and accounts: logon and logout of Microsoft accounts
Author: webabcd
Introduction
Users and accounts of Windows 10
- Logon and cancellation of Microsoft accounts
Example
Demonstrate how to integrate logon and logout of Microsoft accounts into the app
UserAndAccount/MicrosoftAccount. xaml
<Page x: Class = "Windows10.UserAndAccount. microsoftAccount "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: local =" using: Windows10.UserAndAccount "xmlns: d =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "mc: ignorable = "d"> <Grid Background = "Transparent"> <StackPanel Margin = "10 0 10"> <Button Name = "button" Content = "login/logout" Margin = "5" Click = "button_Click"/> <TextBlock Name = "lblMsg" Margin = "5"/> <Ellipse Width = "48" Height = "48" HorizontalAlignment =" left "Margin =" 5 "> <Ellipse. fill> <ImageBrush x: Name = "imagePicture"/> </Ellipse. fill> </Ellipse> </StackPanel> </Grid> </Page>
UserAndAccount/MicrosoftAccount. xaml. cs
/** Demonstrate how to integrate Microsoft account logon and logout into the app ** AccountsSettingsPane-account configuration interface * Show ()-pop-up interface * GetForCurrentView () -Get the current AccountsSettingsPane instance * AccountCommandsRequested-events triggered in the pop-up interface (event parameter: AccountsSettingsPaneCommandsRequestedEventArgs) ** required * GetDeferral () -Get the asynchronous operation object * HeaderText-The title text * Commands to be displayed on the pop-up interface-returns a SettingsCommand list, which is used to add a Custom button on the interface. *** process description: ** Step 1: add the specified web account provider to the Account Logon page. * WebAccountProvider-web account provider (this example uses Microsoft account as an example) * WebAuthenticationCoreManager-used to obtain the specified WebAccountProvider * WebAccountProviderCommand-used to add the specified WebAccountProvider to the Account Logon interface ** step 2 of logon, after you log on to the * WebTokenRequest-obtain the web request object * WebTokenRequestResult through the specified WebAccountProvider-obtain the web Request result through WebAuthenticationCoreManager and the specified WebTokenRequest. ** step 1 of logout, add the specified web account to the Account Management Interface * WebAccount-use WebAuthenticationCoreManager and the specified WebAccountProvider and the specified userId to obtain the web account object * WebAccountCommand-used to add the specified WebAccount to the account in the management interface, ** log out step 2, on the Account Management page, select ** WebAccount-you can call this object's logout method ** Note: * in this example, a ProviderError error is reported, because "you must register your app under the Microsoft Store with a Microsoft developer account" * reminds you that if you want to see the effect, for more information, see MicrosoftAccount in the "" app. xaml */using System; using System. net. http; using System. threading. tasks; using Windows. data. json; using Windows. security. authentication. web. core; using Windows. security. credentials; using Windows. UI. applicationSettings; using Windows. UI. popups; using Windows. UI. xaml; using Windows. UI. xaml. controls; using Windows. UI. xaml. media. imaging; using Windows. UI. xaml. navigation; namespace Windows10.UserAndAccount {public sealed partial class MicrosoftAccount: Page {const string MicrosoftAccountProviderId =" https://login.microsoft.com "; Const string ConsumerAuthority =" consumers "; const string AccountScopeRequested =" wl. basic "; const string AccountClientId =" none "; private string _ userId = null; public MicrosoftAccount () {this. initializeComponent ();} protected override void OnNavigatedTo (NavigationEventArgs e) {base. onNavigatedTo (e); // The Event AccountsSettingsPane triggered on the account configuration page is displayed. getForCurrentView (). accountCommandsRequested + = On AccountCommandsRequested;} protected override void OnNavigatedFrom (NavigationEventArgs e) {base. onNavigatedFrom (e); AccountsSettingsPane. getForCurrentView (). accountCommandsRequested-= OnAccountCommandsRequested;} private void button_Click (object sender, RoutedEventArgs e) {// The account configuration interface AccountsSettingsPane is displayed. show ();} private async void OnAccountCommandsRequested (AccountsSettingsPane sender, Acc OuntsSettingsPaneCommandsRequestedEventArgs e) {AccountsSettingsPaneEventDeferral deferral = e. getDeferral (); if (_ userId = null) {// The account configuration page is displayed to log on to await ShowLoginUI (e); // The title text e to be displayed on the page. headerText = "Please log on";} else {// The account configuration page is displayed for Account Management (this example is used to demonstrate logout) await ShowLogoutUI (e ); // Title text e to be displayed on the pop-up page. headerText = "";} // Add the Custom button e. commands. add (new SettingsCommand ("help", "help", HelpInvoked); e. C Ommands. add (new SettingsCommand ("about", "about", AboutInvoked); deferral. complete ();} private void HelpInvoked (IUICommand command) {lblMsg. text = "the user clicked the" help "button";} private void AboutInvoked (IUICommand command) {lblMsg. text = "click" about ";} // Add the specified web account provider to the Account Logon interface. private async Task ShowLoginUI (AccountsSettingsPaneCommandsRequestedEventArgs e) {WebAccountProvider provider = await WebAuthent IcationCoreManager. findAccountProviderAsync (MicrosoftAccountProviderId, ConsumerAuthority); WebAccountProviderCommand providerCommand = new WebAccountProviderCommand (provider, WebAccountProviderCommandInvoked); e. webAccountProviderCommands. add (providerCommand);} // private async void WebAccountProviderCommandInvoked (WebAccountProviderCommand command) {try {WebTokenRequest webToken Request = new WebTokenRequest (command. webAccountProvider, AccountScopeRequested, AccountClientId); WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager. requestTokenAsync (webTokenRequest); if (webTokenRequestResult. responseStatus = WebTokenRequestStatus. success) {var response = webTokenRequestResult. responseData [0]; if (response! = Null) {// get the userId. In actual development, this information is generally saved to the local string userId = response. webAccount. id; _ userId = userId; lblMsg. text = "Login successful, userId:" + userId; lblMsg. text + = Environment. newLine;/** the logon process of the Microsoft account is now complete * if you want to use response. webAccount. if UserName is used to obtain the account user name or other information, it cannot be obtained * response is required. token requests apis.live.net to obtain the relevant information. The description is as follows * // After the logon is successful, the token to be obtained is as follows: var restApi = new Uri (@" https://apis.live.net/v5.0/ Me? Access_token = "+ response. token); using (var client = new HttpClient () {var infoResult = await client. getAsync (restApi); string content = await infoResult. content. readAsStringAsync ();/* The obtained content is similar to the following {"id": "abcd1234abcd1234", "name": "Wang Lei", "first_name": "lei ", "last_name": "Wang", "link ":" https://profile.live.com/ "," Gender ": null," locale ":" zh_CN "," updated_time ":" 2017-04-27T02: 24: 58 + 0000 "} */var jsonObject = JsonObject. parse (content); string name = jsonObject ["name"]. getString ()?? ""; String locale = jsonObject ["locale"]. GetString ()?? ""; LblMsg. text + = $ "name: {name}, locale: {locale}"; // obtain the user image string profileId = jsonObject ["id"] as follows. getString ()?? ""; String pictureUrl = $" https://apis.live.net/v5.0/ {ProfileId}/picture "?? ""; ImagePicture. imageSource = new BitmapImage (new Uri (pictureUrl) ;}} else {lblMsg. text = "Logon Failed:" + webTokenRequestResult. responseStatus. toString () ;}} catch (Exception ex) {lblMsg. text = ex. toString () ;}}// Add the specified web account to the private async Task ShowLogoutUI (AccountsSettingsPaneCommandsRequestedEventArgs e) {WebAccountProvider provider = await WebAuthenticationCoreManager. findAccount ProviderAsync (MicrosoftAccountProviderId, ConsumerAuthority); WebAccount account = await WebAuthenticationCoreManager. FindAccountAsync (provider, _ userId); if (account! = Null) {// The last parameter is used to specify the function buttons that will appear after the user selects an account (here I test only SupportedWebAccountActions. remove is valid) WebAccountCommand command = new WebAccountCommand (account, WebAccountCommandInvoked, SupportedWebAccountActions. remove); e. webAccountCommands. add (command) ;}else {_ userId = null ;}// the user selects a function in the Account Management Interface private async void WebAccountCommandInvoked (WebAccountCommand command, WebAccountInvokedArgs args) {// if (args. action = WebAccountAction. remove) {await command. webAccount. signOutAsync (); _ userId = null; lblMsg. text = "logout successful"; imagePicture. imageSource = null ;}}}}
OK
[Download source code]