Unable to retrieve user account information in the UWP application. Active directory inside an object (not Azure AD)

In the UWP application, I turned on the User Account Information feature.

I need to get the username and domain name (each of them separately) from the current user (users are logged in with a local Active Directory account - not Azure AD ).

For example, the user must log on to the Active Directory domain domain1 using the username user1 . those. domain1\user1 .

I use the following code to get the necessary data:

 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; 

Questions:

  • strDomainName returns domain1.com\user1 . Why does this include a .com part for all of our domains? In C # domain1\user1 applications, we can easily get domain1\user1 without any problems.
  • strUserName returns an empty string. those. "". 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 you need to enable that is similar to the User Account Information feature? Or are there any other permissions that must be granted to the application to receive this information?

I understand that I can get the value of strDomainName and execute string functions to get what I need. But I want to know if there is a way to get this information directly. Also curious is why KnownUserProperties.AccountName and the other properties listed above, such as FirstName , LastName , etc. Just returns an empty string.

I am running the following version of Windows:

Windows version

I have the following set as Target version and Min Version :

Target Version and Min Version

To test, I also tested using Microsoft's sample UserInfo project from GitHub , and I got the following output:

UserInfo Sample project output

In Settings > Privacy > Account Info , the following was automatically enabled.

TestApp is the application I tried with, and User Info C# Sample is an example application from GitHub:

Settings-Privacy-> AccountInfo

Update:

After enabling Enterprise Authentication KnownUserProperties.PrincipalName returns the expected value. those. user1@domain1.com . However, the other properties listed above, such as FirstName , LastName , etc., just returns an empty string, and I still cannot find any property that returns domain1\user1 (without the .com part)

+7
c # uwp active-directory userinfo
source share
1 answer

The information you are trying to obtain is not reliable, since they (as you mentioned) should not be installed, and may also be limited by access to the privacy settings in general.

I had a similar problem and I would advise you to use the UWP OneDrive API

 using Microsoft.OneDrive.Sdk; 

and then request the wl.basic . This area contains at least a reliable username.

 public static async Task<bool> GetAuthenticatedClient() { string oneDriveConsumerBaseUrl = "https://api.onedrive.com/v1.0"; var scopes = new List<string> { "wl.signin", "wl.basic", }; Task authTask; var onlineIdAuthProvider = new OnlineIdAuthenticationProvider(scopes.ToArray()); authTask = onlineIdAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync(); oneDriveClient = new OneDriveClient(oneDriveConsumerBaseUrl, onlineIdAuthProvider); AuthProvider = onlineIdAuthProvider; try { await authTask; if (!AuthProvider.IsAuthenticated) { return false; } } catch (ServiceException exception) { // Swallow the auth exception but write message for debugging. //Debug.WriteLine(exception.Error.Message); return false; } return true; } 

As for the domain, I'm not sure, but you can try to access it through Environment.UserDomainName , as described in MSDN or with Windows.Networking.Connectivity.NetworkInformation.GetHostNames() , as described here .

0
source share

All Articles