How can I get username or id of currently logged i

2019-08-21 16:21发布

I found this article: Get The Username/User Id Of Currently Logged In User In A UWP App

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

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

Thanks

标签: c# uwp
2条回答
戒情不戒烟
2楼-- · 2019-08-21 17:01
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;
}
查看更多
唯我独甜
3楼-- · 2019-08-21 17:04

Did enable the User Account Information in the package.appxmanifest ?enter image description here

Here is the image which shows the manifest file.

查看更多
登录 后发表回答