-1

As shown below, my Azure Portal is correctly displaying the Source column value as Windows Server AD for the users that were migrated from Windows Active Directory to Azure Active Directory.

Users shown in Azure Portal:

enter image description here

Now in my WPF app, I am using Microsoft Graph to display the same list in a DataGrid. But the following LINQ query is still displaying the Source column value of the migrated users as Azure Active Directory instead of Windows Server AD:

var users = await graphClient.Users
    .Request()
    .Select("displayName, userPrincipalName, onPremisesUserPrincipalName, userType")
    .GetAsync();

List<User> lstUsers = (List<User>)users.CurrentPage.ToList();
//I noticed the following Linq without lambda works better if select case staement have multiple conditions: https://stackoverflow.com/a/936136/1232087
var userslist =
        (
            from User in lstUsers
            select new
            {
                DisplayName = User.DisplayName,
                UserPrincipalName = User.UserPrincipalName,
                OnPremisesUserPrincipalName = User.OnPremisesUserPrincipalName,
                UserType = User.UserType,
                Source =
                (
                    User.UserType == "Member" && !User.UserPrincipalName.Contains("#EXT#") ? "Azure Active Directory" :
                    User.UserType == "Member" && User.UserPrincipalName.Contains("#EXT#") ? "Microsoft Account" :
                    User.UserType == "Guest" && User.ExternalUserState == "Accepted" ? "External Azure Active Directory" :
                    (User.UserType == "Member" && string.IsNullOrEmpty(User.OnPremisesUserPrincipalName) == false) ? "Windows Server AD" :
                    User.UserType == "Guest" && User.ExternalUserState == "PendingAcceptance" ? "Invited user" : "Unknown"
                )
            }
        );

DataGrid display of the above query result in my WPF app:

According to above LINQ query, values inside red should have displayed as Windows Server AD because OnPremisesUserPrincipalName value of the query (shown in On Prem Name column below) are not null

enter image description here

Question: Why the above LINQ query is returning the Source column value as Azure Active Directory instead of Windows Server AD. This seems to be a LINQ related issue unless I'm missing something here. There are similar LINQ examples here and here.

nam
  • 21,967
  • 37
  • 158
  • 332
  • 1
    For those rows does the `UserPrincipalName` contain "#EXT#"? If not, then it's going to pick "Azure Active Directory" because it come first. Maybe you need to move the Windows Server AD check to the beginning? – juharr Aug 01 '20 at 16:51
  • @juharr Your suggestion worked (thank you). You may want to write a `Response` explaining the reason(s) that causing the issue and how it can be resolved. That may help some other readers of the post, as well. And, I'll mark your response as an `Answer`. – nam Aug 01 '20 at 17:15

1 Answers1

1

The order that you do your comparisons in matters. It will stop on the first one that matches, not the last one. So if you want user type Member with no OnPremisesUserPrincipalName to select "Windows Server AD" reguardless of what UserPrincipalName is then you need to move that check up to the first one. Additionally since you already check for "#EXT#" in UserPrincipalName you don't have to check it again on the next line.

Source =
(
    (User.UserType == "Member" && !string.IsNullOrEmpty(User.OnPremisesUserPrincipalName)) 
        ? "Windows Server AD" :
    User.UserType == "Member" && !User.UserPrincipalName.Contains("#EXT#") 
        ? "Azure Active Directory" :
    User.UserType == "Member" 
        ? "Microsoft Account" :
    User.UserType == "Guest" && User.ExternalUserState == "Accepted" 
        ? "External Azure Active Directory" :        
    User.UserType == "Guest" && User.ExternalUserState == "PendingAcceptance" 
        ? "Invited user" : "Unknown"
)
juharr
  • 31,741
  • 4
  • 58
  • 93
  • Agreed. In fact `User.UserType == "Member" && !User.UserPrincipalName.Contains("#EXT#") ` probably covers multiple cases since multiple user types are `member` and do not contain `#EXT#`. And for that reason, probably this condition should be moved to the second last position since 'NOT` scenario is way to broad and hence may cover many scenarios. – nam Aug 01 '20 at 17:33