背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证
[源码下载]
作者:webabcd
介绍
背水一战 Windows 10 之 用户和账号
- 数据账号的添加和管
- OAuth 2.0 验证
示例
1、演示数据账号的添加和管理
UserAndAccount/DataAccount.xaml
<Pagex: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 10"><TextBlock Name="lblMsg" Margin="5" /><Button Name="buttonAdd" Content="新增一个数据账号" Margin="5" Click="buttonAdd_Click" /></StackPanel></Grid> </Page>
UserAndAccount/DataAccount.xaml.cs
/** 演示数据账号的添加和管理* * UserDataAccountManager - 数据账号管理器* ShowAddAccountAsync() - 弹出账号添加界面* ShowAccountSettingsAsync() - 弹出账号管理界面* RequestStoreAsync() - 返回当前用户的数据账号存储区域* GetForUser() - 返回指定用户的数据账号存储区域(通过返回的 UserDataAccountManagerForUser 对象的 RequestStoreAsync() 方法)* * UserDataAccountStore - 数据账号存储区域* FindAccountsAsync() - 返回所有的数据账号* GetAccountAsync() - 返回指定的数据账号* * UserDataAccount - 数据账号* UserDisplayName - 用户名* Id - 数据账号在本地设备上的唯一标识* SaveAsync() - 保存* DeleteAsync() - 删除* ... 还有很多其他属性和方法* * * 注:根据使用的功能需要在 Package.appxmanifest 做相关配置* 1、用到 Windows.System.User 的话需要配置 <Capability Name="userAccountInformation" />* 2、还可能需要 <Capability Name="appointments" />, <Capability Name="contacts" />*/using System; using System.Linq; using System.Collections.Generic; using Windows.ApplicationModel.UserDataAccounts; 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);// 获取当前用户下的全部数据账号UserDataAccountStore store = await UserDataAccountManager.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){// 弹出账号添加界面,如果添加成功会返回新建的数据账号的在本地设备上的唯一标识string userDataAccountId = await UserDataAccountManager.ShowAddAccountAsync(UserDataAccountContentKinds.Email | UserDataAccountContentKinds.Appointment | UserDataAccountContentKinds.Contact);if (string.IsNullOrEmpty(userDataAccountId)){lblMsg.Text += "用户取消了或添加账号失败";lblMsg.Text += Environment.NewLine;}else{UserDataAccountStore store = await UserDataAccountManager.RequestStoreAsync(UserDataAccountStoreAccessType.AllAccountsReadOnly);if (store != null){// 通过数据账号在本地设备上的唯一标识来获取 UserDataAccount 对象UserDataAccount account = await store.GetAccountAsync(userDataAccountId);lblMsg.Text += "新增的数据账号:" + account.UserDisplayName;}}}} }
2、演示如何开发一个基于 OAuth 2.0 验证的客户端
UserAndAccount/OAuth20.xaml
<Pagex: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="登录新浪微博,并返回登录用户好友最新发布的微博" Margin="5" Click="buttonWeibo_Click" /><TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5" /></StackPanel></Grid> </Page>
UserAndAccount/OAuth20.xaml.cs
/** 演示如何开发一个基于 OAuth 2.0 验证的客户端 * 关于 OAuth 2.0 协议请参见:http://tools.ietf.org/html/draft-ietf-oauth-v2-20* * WebAuthenticationBroker - 用于 OAuth 2.0 验证的第一步,可以将第三方 UI 无缝整合进 app* AuthenticateAsync(WebAuthenticationOptions options, Uri requestUri, Uri callbackUri) - 请求 authorization code,返回一个 WebAuthenticationResult 类型的数据** WebAuthenticationResult - 请求 authorization code(OAuth 2.0 验证的第一步)的结果* ResponseData - 响应的数据 * ResponseStatus - 响应的状态* * * 注:本例以微博开放平台为例*/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 = "652ec0b02f814d514fc288f3eab2efda";var callbackUrl = "http://webabcd.cnblogs.com"; // 在新浪微博开放平台设置的回调页var requestAuthorizationCode_url =string.Format("https://api.weibo.com/oauth2/authorize?client_id={0}&response_type=code&redirect_uri={1}",appKey,callbackUrl);// 第一步:request authorization codeWebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None,new Uri(requestAuthorizationCode_url),new Uri(callbackUrl));// 第一步的结果lblMsg.Text = WebAuthenticationResult.ResponseStatus.ToString() + Environment.NewLine;if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success){// 从第一步返回的数据中获取 authorization codevar 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);// 第二步:request access tokenHttpClient client = new HttpClient();var response = await client.PostAsync(new Uri(requestAccessToken_url), null);// 第二步的结果:获取其中的 access tokenvar 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);// 第三步:request protected resource,获取需要的数据(本例为获取登录用户好友最新发布的微博)var result = await client.GetStringAsync(new Uri(requestProtectedResource_url)); // 由于本 app 没有提交微博开放平台审核,所以如果使用的账号没有添加到微博开放平台的测试账号中的话,是会出现异常的lblMsg.Text += "result: " + result;}}catch (Exception ex){lblMsg.Text += Environment.NewLine;lblMsg.Text += ex.ToString();}}/// <summary>/// 模拟 QueryString 的实现/// </summary>/// <param name="queryString">query 字符串</param>/// <param name="key">key</param>private string QueryString(string queryString, string key){return Regex.Match(queryString, string.Format(@"(?<=(\&|\?|^)({0})\=).*?(?=\&|$)", key), RegexOptions.IgnoreCase).Value;}} }/** OAuth 2.0 的 Protocol Flow+--------+ +---------------+| |--(A)- 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
[源码下载]
背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证相关推荐
- 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换...
背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 原文:背水一战 Windows 10 ...
- 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容...
原文:背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容 [源码下载 ...
- 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
原文:背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ...
- 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation
原文:背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation [源码下载] 背水一战 Windows 10 (55 ...
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue...
原文:背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue [源码下载] 背水一战 ...
- 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 原文:背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 [源码 ...
- 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox
背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 原文:背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox [源码 ...
- 背水一战 Windows 10 (41) - 控件(导航类): Frame
原文:背水一战 Windows 10 (41) - 控件(导航类): Frame [源码下载] 背水一战 Windows 10 (41) - 控件(导航类): Frame 作者:webabcd 介绍 ...
- 背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing
背水一战 Windows 10 (34) - 控件(进度类): RangeBase, Slider, ProgressBar, ProgressRing 原文:背水一战 Windows 10 (34) ...
最新文章
- TPAMI 2021 | 深度赋智AutoDL系列竞赛世界冠军方案首次公开
- 认识下PHP如何使用 phpmailer 发送电子邮件
- Oracle控制文件的重建
- 网速不给力,我们自己给——MinGW的手动安装与配置
- 循环前增加和后增加之间的区别?
- python的16.1节课后练习16-2比较希特卡和死亡谷的气温的问题,最后图像为什么出现乱码?
- Cutting Bamboos(牛客多校第九场H主席树+二分+思维)
- Django Step by Step中文版,推荐一下
- linux 6.5桌面环境kde,CentOS 5/6 安装 GNOME 或 KDE 桌面
- 飞秋局域网聊天是一种技术工作
- python环境配置,windows系统,anaconda集成开发环境
- 更新卡住解决_iPhone手机无法正常下载/安装应用的解决方法
- 【转】Build Your own Simplified AngularJS in 200 Lines of JavaScript
- js-ES6学习笔记-Class(6)
- Git学习总结(1)——简介与基本操作
- java MAVEN下载的代码仓库位置 mac
- C#语言和doNet框架
- Alpha版本冲刺(五)
- 深入理解git push
- 深度分析:区块链技术未来发展的 8 个趋势