简体   繁体   English

Azure AD:如何在不使用任何库的情况下访问Azure AD Graph API

[英]Azure AD: How to access Azure AD Graph API without using any library

I have developed a asp.net MVC 4 project .Currently i am planning to access Azure Graph API to get AD User data (access navigation properties of user , Ref link : Azure Rest API Reference ). 我已经开发了一个asp.net MVC 4项目。当前,我正计划访问Azure Graph API以获取AD用户数据(用户的访问导航属性,参考链接: Azure Rest API参考 )。

I have checked Azure AD Graph helper Library , but using this i am not able to access Navigation propery (ie: accessing manager property of User) 我已经检查了Azure AD Graph助手库,但是使用此库我无法访问导航属性(即:访问User的manager属性)

Any help is appreciated. 任何帮助表示赞赏。

By default not all navigation properties would be loaded when you are getting some entity (in our case 'User'). 默认情况下,当您获取某个实体(在我们的示例中为“用户”)时,并非所有导航属性都将被加载。 You should use Expand(..). 您应该使用Expand(..)。 Below I give an example how to assign Manager to User and how to get User's Manager navigation property (some infrastructure nuances omitted): 下面我给出一个示例,如何将Manager分配给User以及如何获取User's Manager导航属性(省略了一些基础设施的细微差别):

public async Task<Result> AssingUserManager(string userUpn, string managerUpn)
{
    try
    {
        var user = (AD.User)await ADClient.Users
            .Where(x => x.UserPrincipalName == userUpn)
            .ExecuteSingleAsync();

        var manager = (AD.User)await ADClient.Users
            .Where(x => x.UserPrincipalName == managerUpn)
            .ExecuteSingleAsync();

        user.Manager = manager;

        await manager.UpdateAsync();
        return Result.Ok();
    }
    catch (Exception ex)
    {
        return Result.Fail(new Error("Failed to assign manager", null, ex));
    }
}

public async Task<Result<User>> GetUserManager(string upn)
{
    try
    {
        var user = (AD.User)await ADClient.Users
                    .Where(x => x.UserPrincipalName == upn)
                    .Expand(x => x.Manager)
                    .ExecuteSingleAsync();

        var manager = user.Manager as AD.User;
        if (manager != null)
        {
            return Result.Ok(ConvertToModel(manager));
        }

        return Result.Fail<User>(new Error("Manager not found for specified user", null));
    }
    catch (Exception ex)
    {
        return Result.Fail<User>(new Error("Failed to get user's manager", null, ex));
    }
}

Also, notice that when I update the graph I do so by calling UpdateAsync on the user who is being assigned as the manager and not the user whose Manager property is being set (taken from this source ) 另外,请注意,在更新图形时,我是通过对被分配为管理员的用户而不是已设置其Manager属性的用户(从此源获取 )调用UpdateAsync来进行的

You can easily hit the Graph directly via HttpClient or any other generic http request generation class. 您可以直接通过HttpClient或任何其他常规的HTTP请求生成类轻松点击Graph。 You just need to stick with OData conventions for accessing specific entities and filter your results. 您只需要遵守OData约定即可访问特定实体并过滤结果。 For some common queries you can try directly without any Graph library take a look at http://msdn.microsoft.com/en-us/library/azure/jj126255.aspx 对于某些常见查询,您可以在没有任何图形库的情况下直接尝试看看http://msdn.microsoft.com/zh-cn/library/azure/jj126255.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Azure AD Graph API Client Library 2.0使用LINQ查找具有特定许可证的Azure AD用户 - How can you use LINQ to find Azure AD users with specific licenses using the Azure AD Graph API Client Library 2.0 如何使用 Azure AD 图表 ZDB974238714CA8DE6434A7CE1DZ08A 创建 API scope - How to create a API scope using Azure AD Graph API 使用图 API 查询 Azure AD - Querying Azure AD using Graph API 如何在不使用Microsoft页面的情况下在Azure AD B2C中使用graph api进行社交登录? - How to use graph api for social login in Azure AD B2C without using Microsoft page? Azure AD - 在 C# Web API 中使用来自 Angular SPA 的图 API 访问令牌 - Azure AD - Using Graph API access token from Angular SPA in C# Web API 使用不带代码隐藏的Azure AD - Using Azure AD without codebehind 使用用户分配的托管标识访问 Azure AD B2C 和 MS Graph API - Access Azure AD B2C with MS Graph API using User-Assigned Managed Identity 是否可以使用用户名和密码对图形 API 进行身份验证,而无需在 Azure AD 中注册应用程序 - Is there way to authenticate to Graph API using Username and Password without Application Registration in Azure AD Azure AD Graph API - 使用C#将用户分配给应用程序 - Azure AD Graph API - Assign user to applications using C# azure 广告让所有用户使用图 api - azure ad get all users using graph api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM