在Azure AD中按名称查找用户

时间:2017-11-23 12:44:19

标签: azure azure-active-directory microsoft-graph

我有这种方法在AAD中验证我的应用程序并通过电子邮件读取用户唯一ID。目前,我尝试从AAD中读取有关用户的任何信息。

    private static async Task AddAzureUserFromEmail2(string email) {
        email = email.ToLowerInvariant();
        var client = new Microsoft.Graph.GraphServiceClient(
            "https://graph.windows.net/xyz.onmicrosoft.com",
            new DelegateAuthenticationProvider(
                async (request) => {
                    ClientCredential clientCred = new ClientCredential(
                        "cf4a6f4e-8b3f-4fdb-4450-19e9caa86123", // ID of app
                        "y728bjhjdfetrEsggddaauuyyttrreehjdffffdfdf="); // secret of app
                    var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/xyz.onmicrosoft.com", false);
                    var authenticationResult = await authenticationContext.AcquireTokenAsync("cf4a6f4e-8b3f-4fdb-4450-19e9caa86123", clientCred);
                    request.Headers.Authorization = new AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);
                }));


        try {
            var user = await client.Users.Request().Select("mail").GetAsync();
        }
        catch (Exception ex) {
            //Here is error.
        }
    }

在try-catch块中,我得到了

  

“代码:generalException消息:从中返回意外异常   服务。 “

怎么了?

2 个答案:

答案 0 :(得分:2)

请查看Microsoft Graph user资源的文档和架构(请参阅架构https://graph.microsoft.com/v1.0/ $ metadata OR documentation https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/user)。

此外,Graph Explorer是一个用于试用Microsoft Graph的巨大交互式工具。它有点像Postman,但专门用于Microsoft Graph。

email资源上没有user属性。有一个mail属性设置为用户的主邮件地址。但是,只有在本地设置此值并同步到云或者为用户分配了O365许可证(和云邮箱)时,才会设置此项。

希望这有帮助,

答案 1 :(得分:0)

我解决了这个问题: 我需要使用" https://graph.microsoft.com"作为资源名称。

var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/dnv.onmicrosoft.com")
相关问题