背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意
作者:webabcd
介绍
背水一战 Windows 10 之 用户和账号
- 获取用户的信息
 - 获取用户的同意
 
示例
1、演示如何获取用户的信息
UserAndAccount/UserInfo.xaml
<Page
x:Class="Windows10.UserAndAccount.UserInfo"
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" /> <Image x:Name="imageProfile" Margin="5" Width="64" Height="64" HorizontalAlignment="Left" /> </StackPanel>
</Grid>
</Page>
UserAndAccount/UserInfo.xaml.cs
/*
* 演示如何获取用户的信息
*
* 需要在 Package.appxmanifest 中的“功能”中勾选“用户账户信息”,即 <Capability Name="userAccountInformation" />
* 如上配置之后,即可通过 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<User> users = await User.FindAllAsync(); // 系统会自动弹出权限请求对话框
User user = users?[];
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
<Page
x:Class="Windows10.UserAndAccount.UserVerifier"
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="buttonRequestConsent" Content="获取用户的同意" Click="buttonRequestConsent_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
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
[源码下载]
背水一战 Windows 10 (82) - 用户和账号: 获取用户的信息, 获取用户的同意的更多相关文章
- 背水一战 Windows 10 (84) - 用户和账号: 微软账号的登录和注销
		
[源码下载] 背水一战 Windows 10 (84) - 用户和账号: 微软账号的登录和注销 作者:webabcd 介绍背水一战 Windows 10 之 用户和账号 微软账号的登录和注销 示例演示 ...
 - 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证
		
[源码下载] 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证 作者:webabcd 介绍背水一战 Windows 10 之 用户和账号 数 ...
 - 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理
		
[源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...
 - 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图
		
[源码下载] 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获 ...
 - 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图
		
[源码下载] 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获取文件夹的属性 ...
 - 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件
		
[源码下载] 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件 作者:webabcd 介绍背水一战 Windows 10 之 ...
 - 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口
		
[源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...
 - 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用
		
[源码下载] 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用 作者:weba ...
 - 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
		
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
 
随机推荐
- CentOS(十二)--crontab命令的使用方法
			
crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. 在 ...
 - python中类与对象及其绑定方法的定义
			
面向对象编程 什么是面向对象? 面向过程:将需要解决的问题按步骤划分,一步一步完成每一个步骤,而且 步骤之间有联系. 优点:复杂问题可以分步完成 缺点:扩展性很差,维护性差.如果中间 ...
 - spring的ioc与aop原理
			
ioc(反向控制) 原理: 在编码阶段,既没有实例化对象,也没有设置依赖关系,而把它交给Spring,由Spring在运行阶段实例化.组装对象.这种做法颠覆了传统的写代码实例化.组装对象.然后一 ...
 - 网络抓包工具 wireshark 入门教程
			
Wireshark Wireshark(前称Ethereal)是一个网络数据包分析软件.网络数据包分析软件的功能是截取网络数据包,并尽可能显示出最为详细的网络数据包数据.Wireshark使用WinP ...
 - eclipse git(版本回退)
			
https://www.cnblogs.com/duex/p/6389999.html
 - Netty4.0源码解析 NioServerSocketChannel
			
一.引言Netty的Channel在JDK NIO的Channel基础上做了一层封装,提供了更多的功能.Netty的中的Channel实现类主要有:NioServerSocketChannel(用于服 ...
 - 好看的alert弹出框sweetalert
			
转载:https://www.cnblogs.com/lamp01/p/7215408.html
 - Django 表关系
			
1.自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义2.order_by asc desc 1. 表关系的创建- OneToOne student = ...
 - vue上线后,背景图片路径错误
			
build 下的utils.js中添加配置 if (options.extract) { return ExtractTextPlugin.extract({ use: loaders, public ...
 - python的序列类
			
1,我们常见的数据结构有哪些是序列类 序列类型的分类: ① 容器序列:list,tuple,deque(可以防止任意的类型的容器) ② 扁平序列:str,bytes,bytearray,array ...