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

一笑奈何 提交于 2019-12-08 19:01:26

问题


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:

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. 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)


回答1:


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.



来源:https://stackoverflow.com/questions/42952503/unable-to-get-some-user-account-information-in-uwp-app-in-premise-active-direc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!