windows 10 在 windows 8.1 商店应用中的 AD 用户名

AD username on windows 10 in a windows 8.1 store app

我正在开发需要与 windows10.

兼容的 8.1 应用程序

我正在尝试获取登录用户的用户名,但它总是 return 是一个 emtry 字符串。

如何在 windows 10 pc 上的 windows 8.1 商店应用程序 运行 中获取用户的用户名?

Windows.System.UserProfile.UserInformation.GetPrincipalNameAsync()

Windows.System.UserProfile.GetDomainNameAsync()

总是return一个空字符串

是否有其他方法获取用户的 AD 用户名?

UserInformation class 文档的 备注 部分所述:

Important The UserInformation class is not supported on Windows 10 or later. Use the User class instead.

Windows 10:

Apps compiled for Windows 8 that use the UserInformation class no longer return user information when running on Windows 10. This is because in Windows 10, apps do not have access to user information without explicit user consent, unlike in Windows 8 where this permission is granted by default.

If you have a Windows 8 app that uses the UserInformation class, you should migrate your app to the Universal Windows Platform (UWP) and access the User class instead. Universal Windows Platform (UWP) apps that access user information are now expected to declare a new capability, uap:userAccountInformation, and call new APIs, Windows.System.User.FindAllAsync and User.GetPropertiesAsync, to get the data.

如果你在代码中使用UserInformation.NameAccessAllowed 属性,你会发现它的值是false

If this property is false, the GetDisplayNameAsync, GetDomainNameAsync, GetFirstNameAsync, and GetLastNameAsync methods return an empty string, while the GetAccountPicture and GetSessionInitiationProtocolUriAsync methods return a null value.

所以我认为要在 Windows 10 上获取用户名,您必须将您的应用程序迁移到通用 Windows 平台 (UWP) 并访问 User class instead. For details, you can check the User information sample GitHub.