如何在 UWP App 中获取当前登录用户的用户名或 ID [英] How can I get username or id of currently logged in user in UWP App

查看:36
本文介绍了如何在 UWP App 中获取当前登录用户的用户名或 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这篇文章:获取 UWP 应用中当前登录用户的用户名/用户 ID

获取当前登录 uwp app 的用户的用户名,但是当我尝试接受的答案时,它返回 null/empty ,是否有任何更新版本?

to get the username of current user logged in in uwp app , but when I tried the accepted answer , it returns null/empty , is there any updated version of that?

注意:我使用的是微软帐户

NOTE: Iam using a microsoft account

更新:KnownUserProperties"中的所有属性都返回空字符串,除了First Name" &姓氏"

UPDATE: All properties from "KnownUserProperties" returns empty string except for "First Name" & "Last Name"

谢谢

推荐答案

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    IReadOnlyList<User> users = await User.FindAllAsync();

    var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated && 
                                p.Type == UserType.LocalUser).FirstOrDefault();

    // user may have username
    var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
    string displayName = (string)data;

    text1.Text = displayName;
}

这篇关于如何在 UWP App 中获取当前登录用户的用户名或 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆