Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
164 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...