博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意
阅读量:7235 次
发布时间:2019-06-29

本文共 7740 字,大约阅读时间需要 25 分钟。

原文:

背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意

作者:
介绍
背水一战 Windows 10 之 用户和账号

  • 获取用户的信息
  • 获取用户的同意

示例
1、演示如何获取用户的信息
UserAndAccount/UserInfo.xaml

UserAndAccount/UserInfo.xaml.cs

/* * 演示如何获取用户的信息 *  * 需要在 Package.appxmanifest 中的“功能”中勾选“用户账户信息”,即 
* 如上配置之后,即可通过 api 获取用户的相关信息(系统会自动弹出权限请求对话框) * * User - 用户 * FindAllAsync() - 查找全部用户,也可以根据 UserType 和 UserAuthenticationStatus 来查找用户 * 经过测试,其只能返回当前登录用户 * GetPropertyAsync(), GetPropertiesAsync() - 获取用户的指定属性 * 可获取的属性请参见 Windows.System.KnownUserProperties * GetPictureAsync() - 获取用户图片 * 图片规格有 64x64, 208x208, 424x424, 1080x1080 * NonRoamableId - 用户 id * 此 id 不可漫游 * UserType - 用户类型 * LocalUser, RemoteUser, LocalGuest, RemoteGuest * UserAuthenticationStatus - 用户的身份验证状态 * Unauthenticated, LocallyAuthenticated, RemotelyAuthenticated * CreateWatcher() - 返回 UserWatcher 对象,用于监听用户的状态变化 * 本例不做演示 */using System;using System.Collections.Generic;using Windows.Foundation.Collections;using Windows.Storage.Streams;using Windows.System;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Media.Imaging;using Windows.UI.Xaml.Navigation;namespace Windows10.UserAndAccount{ public sealed partial class UserInfo : Page { public UserInfo() { this.InitializeComponent(); } protected async override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); // 我这里测试的结果是:返回的集合中只有一个元素,就是当前的登录用户 IReadOnlyList
users = await User.FindAllAsync(); // 系统会自动弹出权限请求对话框 User user = users?[0]; if (user != null) { // 对于获取用户的 NonRoamableId, Type, AuthenticationStatus 信息,不同意权限请求也是可以的 string result = "NonRoamableId: " + user.NonRoamableId + "\n"; result += "Type: " + user.Type.ToString() + "\n"; result += "AuthenticationStatus: " + user.AuthenticationStatus.ToString() + "\n"; // 对于获取用户的如下信息及图片,则必须要同意权限请求 string[] desiredProperties = new string[] { KnownUserProperties.DisplayName, KnownUserProperties.FirstName, KnownUserProperties.LastName, KnownUserProperties.ProviderName, KnownUserProperties.AccountName, KnownUserProperties.GuestHost, KnownUserProperties.PrincipalName, KnownUserProperties.DomainName, KnownUserProperties.SessionInitiationProtocolUri, }; // 获取用户的指定属性集合 IPropertySet values = await user.GetPropertiesAsync(desiredProperties); foreach (string property in desiredProperties) { result += property + ": " + values[property] + "\n"; } // 获取用户的指定属性 // object displayName = await user.GetPropertyAsync(KnownUserProperties.DisplayName); lblMsg.Text = result; // 获取用户的图片 IRandomAccessStreamReference streamReference = await user.GetPictureAsync(UserPictureSize.Size64x64); if (streamReference != null) { IRandomAccessStream stream = await streamReference.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); imageProfile.Source = bitmapImage; } } } }}

2、演示如何获取用户的同意
UserAndAccount/UserVerifier.xaml

UserAndAccount/UserVerifier.xaml.cs

/* * 演示如何获取用户的同意 *  * UserConsentVerifier - 验证器(比如 pin 验证等) *     CheckAvailabilityAsync() - 验证器的可用性 *     RequestVerificationAsync(string message) - 请求用户的同意(可以指定用于提示用户的信息) */using System;using Windows.Security.Credentials.UI;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;namespace Windows10.UserAndAccount{    public sealed partial class UserVerifier : Page    {        public UserVerifier()        {            this.InitializeComponent();        }        protected async override void OnNavigatedTo(NavigationEventArgs e)        {            base.OnNavigatedTo(e);            try            {                UserConsentVerifierAvailability verifierAvailability = await UserConsentVerifier.CheckAvailabilityAsync();                switch (verifierAvailability)                {                    case UserConsentVerifierAvailability.Available: // 验证器可用                        lblMsg.Text = "UserConsentVerifierAvailability.Available";                        break;                    case UserConsentVerifierAvailability.DeviceBusy:                        lblMsg.Text = "UserConsentVerifierAvailability.DeviceBusy";                        break;                    case UserConsentVerifierAvailability.DeviceNotPresent:                        lblMsg.Text = "UserConsentVerifierAvailability.DeviceNotPresent";                        break;                    case UserConsentVerifierAvailability.DisabledByPolicy:                        lblMsg.Text = "UserConsentVerifierAvailability.DisabledByPolicy";                        break;                    case UserConsentVerifierAvailability.NotConfiguredForUser:                        lblMsg.Text = "UserConsentVerifierAvailability.NotConfiguredForUser";                        break;                    default:                        break;                }            }            catch (Exception ex)            {                lblMsg.Text = ex.ToString();            }            lblMsg.Text += "\n";        }        private async void buttonRequestConsent_Click(object sender, RoutedEventArgs e)        {            try            {                UserConsentVerificationResult consentResult = await UserConsentVerifier.RequestVerificationAsync("我要做一些操作,您同意吗?");                switch (consentResult)                {                    case UserConsentVerificationResult.Verified: // 验证通过                        lblMsg.Text += "UserConsentVerificationResult.Verified";                        break;                    case UserConsentVerificationResult.DeviceBusy:                        lblMsg.Text += "UserConsentVerificationResult.DeviceBusy";                        break;                    case UserConsentVerificationResult.DeviceNotPresent:                        lblMsg.Text += "UserConsentVerificationResult.DeviceNotPresent";                        break;                    case UserConsentVerificationResult.DisabledByPolicy:                        lblMsg.Text += "UserConsentVerificationResult.DisabledByPolicy";                        break;                    case UserConsentVerificationResult.NotConfiguredForUser:                        lblMsg.Text += "UserConsentVerificationResult.NotConfiguredForUser";                        break;                    case UserConsentVerificationResult.RetriesExhausted:                        lblMsg.Text += "UserConsentVerificationResult.RetriesExhausted";                        break;                    case UserConsentVerificationResult.Canceled: // 验证取消                        lblMsg.Text += "UserConsentVerificationResult.Canceled";                        break;                    default:                        break;                }            }            catch (Exception ex)            {                lblMsg.Text += ex.ToString();            }            lblMsg.Text += "\n";        }    }}

OK

转载地址:http://gwmfm.baihongyu.com/

你可能感兴趣的文章
linux 文件查找帮助命令 , 查看网络链接信息, 历史命令
查看>>
centos 分区扩容
查看>>
ActiveMQ Tips
查看>>
linux日常维护(网络相关,防火墙,netfirter介绍,netfirter语法)
查看>>
keepalived双机热备nginx
查看>>
深入Nginx优化
查看>>
Collections的简单学习
查看>>
简介Valgrind工具包功能
查看>>
Chrome开发者工具的小技巧
查看>>
LINUX系统服务与管理(Services)---------第六天
查看>>
HA之Drbd
查看>>
Javascrip中获取页面路径
查看>>
VS项目工程属性编排
查看>>
关于epel源的配置
查看>>
系统启动之后将/var/log挂载到独立的分区
查看>>
资源池的两个小教训
查看>>
python pip 修改源
查看>>
中国联通与用友签署合作框架协议 多领域展开全面合作
查看>>
RDO CentoS 7.1部署 kilo 问题及处理办法
查看>>
hashlib模块
查看>>