Unable to get some user account information in UWP app - In-premise active directory (Not Azure AD)
Asked Answered
L

2

8

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 returns domain1.com\user1. Why does this include the .com part for all our domains? On c# winforms applications we can easily get domain1\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:

Windows Version

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

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:

UserInfo Sample project 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:

Settings-Privacy->AccountInfo

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)

Laflam answered 22/3, 2017 at 13:5 Comment(0)
O
0

The Information you are trying to access are not reliable, as they (as you mentioned) do not have to be set and also they can be restricted access to via privacy settings in general.

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

using Microsoft.OneDrive.Sdk;

and then request wl.basic scope. This scope 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 could try to access it via Environment.UserDomainName like described on MSDN or with Windows.Networking.Connectivity.NetworkInformation.GetHostNames() like described here.

Onceover answered 28/3, 2017 at 8:9 Comment(0)
L
0

I found another possible solution to this. If you are still debugging this locally or the app was already installed on the target machine, I could enable the capabality User Account Information but this was not propagated to the actual installed app:

In VS everything is set up

when your search for your app name in the start menu and then right click the entry and select App settings you get something like this:

Properties of the installed app on German Windows

As soon as I enabled the highlighted option (basically it says 'Account information') it worked.

Liddle answered 16/12, 2020 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.