World War I Windows 10 (83) and World War I 83
[Download source code]
Backwater world war I Windows 10 (83)-users and accounts: Add and manage data accounts, and verify OAuth 2.0
Author: webabcd
Introduction
Users and accounts of Windows 10
- Add and manage data accounts
- OAuth 2.0 Verification
Example
1. demonstrate how to add and manage data accounts
UserAndAccount/DataAccount. xaml
<Page x: Class = "Windows10.UserAndAccount. dataAccount "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"> <TextBlock Name = "lblMsg" Margin = "5"/> <button Name = "buttonAdd" Content = "add a data account" Margin = "5" Click = "buttonAdd_Click"/> </StackPanel> </Grid> </Page>
UserAndAccount/DataAccount. xaml. cs
/** Demonstrate data account addition and Management ** UserDataAccountManager-data account manager * ShowAddAccountAsync ()-The account addition page * ShowAccountSettingsAsync ()-The Account Management Interface * RequestStoreAsync () is displayed () -Return to the storage region of the current user's data account * GetForUser ()-Return to the storage region of the specified user's data account (use the RequestStoreAsync () method of the returned UserDataAccountManagerForUser object) ** UserDataAccountStore-data account storage Region * FindAccountsAsync ()-returns all data Accounts * GetAccountAsync ()-returns the specified data account ** UserDataAccount-data account * Use RDisplayName-user name * Id-Unique Id of the data account on the local device * SaveAsync ()-save * DeleteAsync ()-delete *... there are many other attributes and Methods ** Note: You need to use the Package function. appxmanifest for related configuration * 1. Windows is used. system. <Capability Name = "userAccountInformation"/> * 2. <Capability Name = "appointments"/>, <Capability Name = "contacts"/> */using System; using System. linq; using System. collections. generic; using Windows. applicationModel. U SerDataAccounts; using Windows. UI. xaml; using Windows. UI. xaml. controls; using Windows. UI. xaml. navigation; namespace Windows10.UserAndAccount {public sealed partial class DataAccount: Page {public DataAccount () {this. initializeComponent ();} protected async override void OnNavigatedTo (NavigationEventArgs e) {base. onNavigatedTo (e); // obtain all data accounts of the current user, UserDataAccountStore = await UserDataAccount Manager. requestStoreAsync (UserDataAccountStoreAccessType. allAccountsReadOnly); IReadOnlyList <UserDataAccount> accounts = await store. findAccountsAsync (); lblMsg. text + = string. join (",", accounts. select (p => p. userDisplayName); lblMsg. text + = Environment. newLine;} private async void buttonAdd_Click (object sender, RoutedEventArgs e) {// The add account page is displayed, if the account is successfully added, the system returns the string userDataAccount, the unique identifier of the newly created data account on the local device. Id = await UserDataAccountManager. showAddAccountAsync (UserDataAccountContentKinds. email | UserDataAccountContentKinds. appointment | UserDataAccountContentKinds. contact); if (string. isNullOrEmpty (userDataAccountId) {lblMsg. text + = "the user canceled or failed to add the Account"; lblMsg. text + = Environment. newLine;} else {UserDataAccountStore store = await UserDataAccountManager. requestStoreAsync (UserDataAccountStoreAccessType. AllAccountsReadOnly); if (store! = Null) {// obtain the UserDataAccount object UserDataAccount account = await store by using the unique identifier of the data account on the local device. getAccountAsync (userDataAccountId); lblMsg. text + = "new data account:" + account. userDisplayName ;}}}}}
2. demonstrate how to develop a client based on OAuth 2.0 Verification
UserAndAccount/oauth1_xaml
<Page x: Class = "Windows10.UserAndAccount. OAuth20 "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 10"> <Button Name = "buttonWeibo" Content = "log on to Sina Weibo, and return to the microblog "Margin =" 5 "Click =" buttonWeibo_Click "/> <TextBlock Name =" lblMsg "TextWrapping =" Wrap "Margin =" 5 "/> </StackPanel> </Grid> </Page>
UserAndAccount/oauth1_xaml. cs
/** Demonstrate how to develop a client based on OAuth 2.0 authentication * For OAuth 2.0 protocol, see: http://tools.ietf.org/html/draft-ietf-oauth-v2-20 ** WebAuthenticationBroker-the first step for OAuth 2.0 verification. You can seamlessly integrate third-party UIS into the app * AuthenticateAsync (WebAuthenticationOptions options, Uri requestUri, Uri callbackUri)-request authorization code, return data of the WebAuthenticationResult type ** WebAuthenticationResult-request authorization code (step 1 of OAuth 2.0 verification) * ResponseData-response data * ResponseStatus-response status ** note: this example uses the Weibo open platform as an example */using System; using System. net. http; using System. text. regularExpressions; using Windows. data. json; using Windows. security. authentication. web; using Windows. UI. xaml; using Windows. UI. xaml. controls; namespace Windows10.UserAndAccount {public sealed partial class OAuth20: Page {public OAuth20 () {this. initializeComponent ();} private async void buttonWeibo_Click (object sender, RoutedEventArgs e) {try {var appKey = "39261162"; var appSecret = "secret"; var callbackUrl =" http://webabcd.cnblogs.com "; // Callback page var requestAuthorizationCode_url = string. Format (" https://api.weibo.com/oauth2/authorize?client_id= {0} & response_type = code & redirect_uri = {1} ", appKey, callbackUrl); // Step 1: request authorization code WebAuthenticationResult = await WebAuthenticationBroker. authenticateAsync (WebAuthenticationOptions. none, new Uri (requestAuthorizationCode_url), new Uri (callbackUrl); // The result of the first step is lblMsg. text = WebAuthenticationResult. responseStatus. toString () + Environment. newLine; if (WebAuthenticationResult. responseStatus = WebAuthenticationStatus. success) {// obtain authorization code var authorizationCode = QueryString (WebAuthenticationResult. responseData, "code"); lblMsg. text + = "authorizationCode:" + authorizationCode + Environment. newLine; var requestAccessToken_url = string. format (" https://api.weibo.com/oauth2/access_token?client_id= {0} & client_secret = {1} & grant_type = authorization_code & redirect_uri = {2} & code = {3} ", appKey, appSecret, callbackUrl, authorizationCode ); // Step 2: request access token HttpClient client = new HttpClient (); var response = await client. postAsync (new Uri (requestAccessToken_url), null); // result of step 2: Obtain the access token var jsonString = await response. content. readAsStringAsync (); JsonObject jsonObject = JsonObject. parse (jsonString); var accessToken = jsonObject ["access_token"]. getString (); lblMsg. text + = "accessToken:" + accessToken + Environment. newLine; var requestProtectedResource_url = string. format (" https://api.weibo.com/2/statuses/friends_timeline.json?access_token= {0} ", accessToken); // Step 3: request protected resource to obtain the required data (in this example, retrieve the microblog recently published by a user's friend) var result = await client. getStringAsync (new Uri (requestProtectedResource_url); // because this app is not submitted to the Weibo Open Platform for review, if the account used is not added to the test account of the Weibo open platform, is lblMsg. text + = "result:" + result;} catch (Exception ex) {lblMsg. text + = Environment. newLine; lblMsg. text + = ex. toString () ;}//< summary> /// simulate the implementation of QueryString /// </s Ummary> /// <param name = "queryString"> query string </param> /// <param name = "key"> key </param> private string QueryString (string queryString, string key) {return Regex. match (queryString, string. format (@"(? <= (\&| \? | ^) ({0}) \ = ).*? (? =\& | $) ", Key), RegexOptions. ignoreCase ). value ;}}/ ** Protocol Flow + -------- ++ ----------------- + | -- (A) of OAuth 2.0) -Authorization Request-> | Resource | Owner | <-(B) -- Authorization Grant --- | + --------------- + | -- (C) -- Authorization Grant --> | Authorization | Client | Server | <-(D) ----- Access Token ------- | + --------------- + | -- (E) ----- Access Token ------> | Resource | Server | <-(F) --- Protected Resource --- | + -------- ++ --------------- + */
OK
[Download source code]