背水一戰 Windows 10 之 用戶和賬號: 獲取用戶的信息, 獲取用戶的同意 ...
背水一戰 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?[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
<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
[源碼下載]