In a UWP app, I have enabled the User Account Information
capability.
I need to get the username and the domain name (each of them separately) of the currently logged on user (The users are logged on with an in-premise Active Directory account - Not Azure AD).
For example, the user would log in to the Active Directory domain domain1
using the username user1
. i.e. domain1\user1
.
I am using the following code to try to get the required details:
IReadOnlyList<User> users = await User.FindAllAsync();
var user = users.FirstOrDefault();
// get domain
var data1 = await user.GetPropertyAsync(KnownUserProperties.DomainName);
string strDomainName = (string)data1;
// get username
var data2 = await user.GetPropertyAsync(KnownUserProperties.AccountName);
string strUserName = (string)data2;
Issues:
strDomainName
returnsdomain1.com\user1
. Why does this include the.com
part for all our domains? On c# winforms applications we can easily getdomain1\user1
without any issue.strUserName
returns an empty string. i.e. "". Why does this not return any value?
I also checked the following:
KnownUserProperties.FirstName
returns an empty string. i.e. ""KnownUserProperties.LastName
returns an empty string. i.e. ""KnownUserProperties.PrincipalName
returns an empty string. i.e. ""KnownUserProperties.ProviderName
returns an empty string. i.e. ""KnownUserProperties.GuestHost
returns an empty string. i.e. ""
Is there anything else I need to enable similar to the User Account Information
capability? Or are there any other permissions that need to be granted to the app to get this information?
I understand that I can get the value of strDomainName
and perform string functions to get what I need. But I want to know if there is any way to get this information directly. Also curious why KnownUserProperties.AccountName
and other properties listed above such as FirstName
, LastName
etc. just returns an empty string.
I am running the following version of Windows:
I have the following set as the Target version
and Min Version
:
To verify, I also tested with the UserInfo sample project by Microsoft from GitHub and I got the following output:
The following was automatically enabled in Settings
> Privacy
> Account Info
.
TestApp
is the app I tried with and User Info C# Sample
is the sample app from GitHub:
Update:
After also enabling the Enterprise Authentication
capability, KnownUserProperties.PrincipalName
does return the expected value. i.e. [email protected]
.
However, other properties listed above such as FirstName
, LastName
etc. just returns an empty string and I am still unable to find any property that returns domain1\user1
(without the .com
part)