【问题标题】:MS Graph: Azure AD - How to find the Source column in the users list?MS Graph:Azure AD - 如何在用户列表中找到源列?
【发布时间】:2020-07-15 03:53:14
【问题描述】:

我正在使用Microsoft Graph 检索使用Azure AD 的组织中的用户列表。当我以全局管理员身份登录Azure Portal 并单击Azure AD-->Users 时,它会显示如下用户列表,其中最后一列(以红色显示)是Source 列:

问题:如何从list of users 中获取Source 列?默认情况下,only a limited set of properties are returnedbusinessPhonesdisplayNamegivenNameidjobTitlemailmobilePhoneofficeLocationpreferredLanguagepreferredLanguage、@987654331 @)。

【问题讨论】:

    标签: c# azure azure-active-directory microsoft-graph-api microsoft-graph-sdks


    【解决方案1】:

    据我所知,graph api 的响应中没有source 字段。我们可以在这个page中看到user的所有属性,并且不存在source的属性。

    Azure 门户请求另一个 api 但不是图形 api(列表用户)来显示源。

    顺便说一下,graph api list user(v1.0) 只显示了几个字段。要显示更多字段,可以在api中使用$select(如https://graph.microsoft.com/v1.0/users?$select=displayName,userType)。如果你使用graph api list user(beta),它将显示用户的所有字段。

    希望对你有帮助~

    【讨论】:

    • 您写道:Azure portal request another api but not graph api(list user) to show the source。你碰巧知道anohter api 将是什么来获得Source 值?我需要显示该值(例如,显示用户是否是 Azure AD 用户、MS 帐户用户、受邀用户等(如屏幕截图的 Source 列所示)?
    • 嗨@nam,“另一个” api 是https://main.iam.ad.ext.azure.com/api/Users,如屏幕截图所示。但我认为我们不能要求它,因为它似乎不是提供给我们使用的 api。要请求这个api,我们必须提供token,但是我们没有token,我们无法获取token。
    • @nam 对于您的要求,我认为您可以通过解析用户的其他一些列来实现它。例如,如果 userType 列是 Member,则用户是 Azure AD 用户。如果userTypeGuest 并且externalUserState 是“接受”,则用户是External Azure Active DirectoryexternalUserState 是“PendingAcceptance”,它是Invited user.......
    • 这是个好建议。我实现了它并且它有效(谢谢)。
    • 在更复杂的情况下,我得到一个不正确的结果。我已经发布了问题here
    【解决方案2】:

    如果您还没有使用它,我会查看图形资源管理器 https://developer.microsoft.com/en-us/graph/graph-explorer

    如果我不得不猜测它是一个基于 userPrincipalName 的动态列。如果用户来自外部系统,则 userPrincipalName 中将有 #EXT#。我会看一下,看看它们是否有什么不同。

    我在看端点https://graph.microsoft.com/v1.0/users

    作为 Hury 的评论提到,您可以使用 userType 和 externalUserState 来确定相同的事情。

    如果我在 c# 中执行此操作(这是凭记忆,如果有错字,请见谅)

    public string Source { 
       get {
          return UserPrincipalName.Contains("#EXT#")?"Microsoft Account":"Azure Active Directory";
       }
    }
    
    

    用于更复杂的处理

    private string _source = null;
    public string Source _source??(_source=GenerateSource());
    }
    
    protected string GenerateSource(){
       return UserPrincipalName.Contains("#EXT#")?"Microsoft Account":"Azure Active Directory";
    }
    

    如果我使用的代码类似于可以在此处找到的代码 MS Graph - LINQ query returning incorrect results 我会把它作为扩展类来做(没有从内存中测试过,但我应该很接近)

    
    public static class UserExtension{
        
        public static UserSource(this User user){
            var userTypeUpper = _user.UserType.ToUpperCase();
            var userPrincipalNameUpper = user.UserPrincipalName.ToUpperCase();
            var externalUserStateUpper = user.ExternalUserState.ToUpperCase();
            
            return (_user.UserType == "MEMBER" && userPrincipalNameUpper.Contains("#EXT#") == false) ? "Azure Active Directory" :
                        (userTypeUpper == "MEMBER" && userPrincipalNameUpper.Contains("#EXT#")) ? "Microsoft Account" :
                        (userTypeUpper == "GUEST" && externalUserStateUpper == "ACCEPTED") ? "External Azure Active Directory" :
                        (userTypeUpper == "GUEST" && externalUserStateUpper == "PENDINGACCEPTANCE") ? "Invited user" : "Unknown";
        }
        
    }
    
    //sample code using it
    
    Microsoft.Graph.IGraphServiceUsersCollectionPage users = await graphClient.Users.Request()
        .Select("displayName, userPrincipalName, userType")
        .GetAsync();
    
    List<User> lstUsers = (List<User>)users.CurrentPage.ToList();
    
    var source = lstUsers.First().UserSource()
    
    
    

    【讨论】:

    • userPrincipalName 实际上是上图中的第二列(用户名)。 Azure 门户中的Source 列显示(如上图所示)Active DirectoryMicrosoft Account,这意味着用户要么是 Azure AD 用户,要么是 MS 个人帐户用户。带有GET https://graph.microsoft.com/v1.0/users 请求的Graph Explorer 也只返回我的问题中列出的默认标准列(那里也有官方链接)。
    • 是的,我的回答更多是说源列可能是基于其他信息的动态列,以使其更具可读性。因此,在我的示例中,如果图表中的 userPrincipalName 包含#EXT#,我会知道源是外部的并且是 Microsoft 帐户。我假设所有 Azure AD 用户都是内部用户。即使在您的示例中,唯一具有 Source Microsoft 帐户的用户也具有带有 #EXT# 的 userPrincipalName,而其他用户则没有。然而 externalUserState 和 userType 一起做同样的事情
    • 感谢您的进一步澄清和解释。如果我们只处理两种情况(即`if UserPrincipalName contains #EXT# else .....),包含Source 属性的更新代码是一个很好的实现代码。对于两种以上的情况,它可能需要更多的抛光(我认为)。例如,如果您查看 Hury 的屏幕截图,用户可能是外部用户、受邀用户、MS 帐户等。
    • 我刚刚更新了我的答案以包含一种增加复杂性的方法。可以轻松地将其转换为函数并使用 Hury 的消息中提到的属性并将其作为后备。很多选择。
    • 我们可以在this 场景中应用您更复杂的案例吗?
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    相关资源
    最近更新 更多