0

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

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)

Any help is appreciated.

4

2 回答 2

1

默认情况下,当您获取某个实体(在我们的示例中为“用户”)时,不会加载所有导航属性。您应该使用 Expand(..)。下面我举例说明如何将 Manager 分配给 User 以及如何获取 User 的 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));
    }
}

另外,请注意,当我更新图表时,我通过对被分配为经理的用户而不是正在设置 Manager 属性的用户调用 UpdateAsync 来做到这一点(取自此来源

于 2015-11-20T13:37:24.040 回答
0

您可以通过 HttpClient 或任何其他通用 http 请求生成类轻松地直接点击 Graph。您只需要坚持使用 OData 约定来访问特定实体并过滤您的结果。对于一些常见查询,您可以直接尝试,无需任何图形库,请查看http://msdn.microsoft.com/en-us/library/azure/jj126255.aspx

于 2014-06-24T06:08:31.237 回答