Search code examples
windows-runtimewindows-10-universalwindows-8.1-universal

AD username on windows 10 in a windows 8.1 store app


I'm developing a 8.1 app that needs to be compatible with windows 10.

I'm trying to get the Username of the user logged in, but it always returns a emtry string.

How do i get the username of a user in a windows 8.1 store app running on a windows 10 pc?

Windows.System.UserProfile.UserInformation.GetPrincipalNameAsync()

and

Windows.System.UserProfile.GetDomainNameAsync()

always return an empty string

Is there another way of getting the AD user name of a user?


Solution

  • As the description in the Remarks section of UserInformation class document:

    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.

    And if you use UserInformation.NameAccessAllowed property in your code, you will find its value is 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.

    So I think to get the user name on Windows 10, you have to migrate your app to the Universal Windows Platform (UWP) and access the User class instead. For details, you can check the User information sample at GitHub.