简体   繁体   中英

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

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 . ie 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. ie "". Why does this not return any value?

I also checked the following:

  • KnownUserProperties.FirstName returns an empty string. ie ""
  • KnownUserProperties.LastName returns an empty string. ie ""
  • KnownUserProperties.PrincipalName returns an empty string. ie ""
  • KnownUserProperties.ProviderName returns an empty string. ie ""
  • KnownUserProperties.GuestHost returns an empty string. ie ""

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:

UserInfo 示例项目输出

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. ie user1@domain1.com . 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)

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 .

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:

在 VS 中一切都设置好了

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:

在德语 Windows 上安装的应用程序的属性

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM