简体   繁体   English

如何获取UWP App中当前登录用户的用户名或ID

[英]How can I get username or id of currently logged in user in UWP App

I found this article: Get The Username/User Id Of Currently Logged In User In A UWP App 我找到了这篇文章: 获取UWP应用程序中当前登录用户的用户名/用户ID

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? 获取当前用户的用户名登录uwp应用程序,但是当我尝试接受的答案时,它返回null / empty,是否有该版本的更新版本?

NOTE: Iam using a microsoft account 注意:Iam使用一个Microsoft帐户

UPDATE: All properties from "KnownUserProperties" returns empty string except for "First Name" & "Last Name" 更新:“ KnownUserProperties”中的所有属性都返回空字符串,“ First Name”和“ Last Name”除外

Thanks 谢谢

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;
}

Did enable the User Account Information in the package.appxmanifest ? 是否在package.appxmanifest中启用了用户帐户信息? 在此处输入图片说明

Here is the image which shows the manifest file. 这是显示清单文件的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取UWP App中当前登录用户的用户名/用户ID - Get The Username/User Id Of Currently Logged In User In A UWP App 如何获取UWP中当前登录用户的用户名? - How to get the username of the current logged in user in UWP? 如何使用 UserManager 按 ID、名称、email 或其他搜索条件获取用户(不是当前登录的用户)? - How can I get a user (not the currently logged in user) by Id, name, email or other search criteria, using UserManager? 如何获取ASP.NET Core 2中当前登录用户的用户名,而不是应用程序池详细信息 - How to I get the username of the currently logged in user in ASP.NET Core 2, not Application Pool details 如何获取当前登录用户的会话ID? - How to get Currently logged user's session ID? 如何使用C#通过他的ID获取当前登录用户? - How to get currently logged in user by his ID using C#? 如何获取登录用户的用户名并进行比较以获取ID - How to get the username of a logged in user and compare it to get the id 如何在MVC5中获取当前登录用户的特定字段? - How can I get the specific fields of the currently logged-in user in MVC5? 如何获取当前登录用户的 AD 显示名称 - How do I get the AD Display Name of the currently logged in user 如何在 .NET Core 中检索当前登录用户的 email? - How can I retrieve the email of the currently logged in user in .NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM