如何在不使用OpenID的情况下使用AAD身份验证从AAD检索登录用户的组/角色“ MailNickName”

时间:2019-08-05 09:16:16

标签: azure authentication azure-active-directory azure-web-sites

我已经创建了具有AAD身份验证的Azure Web App。

但无法读取“登录用户”组的详细信息。

我已经创建了https://docs.microsoft.com/en-us/azure/app-service/configure-authentication-provider-aad这样的网络应用/应用注册。

Web应用程序经过身份验证的应用程序重定向https://contoso.azurewebsites.net/.auth/login/done

一旦AAD用户获得授权。我们需要提取组详细信息(我需要MailNickName)。

using (var httpClient = new HttpClient())
                     {
                         httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Request.Headers["X-MS-TOKEN-AAD-ACCESS-TOKEN"]}");
                        var tenantId =  ClaimsPrincipal.Current.Claims.Single(x => x.Type == "http://schemas.microsoft.com/identity/claims/tenantid").Value;
                        var userId =ClaimsPrincipal.Current.Claims.Single(x => x.Type == "http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                         var httpResponse =httpClient.GetAsync("https://graph.microsoft.com/v1.0/me/memberOf").Result;
                         httpResponse.EnsureSuccessStatusCode();
                         var jsonResult =await httpResponse.Content.ReadAsStringAsync(); 
                      /*  IUserMemberOfCollectionWithReferencesPage groups;
                        while (groups.Count > 0)
                        {
                            foreach (Group g in groups)
                            {
                                rol.Add(g.MailNickname);
                            }
                            if (groups.NextPageRequest != null)
                            {
                                groups = await groups.NextPageRequest.GetAsync();
                            }
                            else
                            {
                                break;
                            }
                        }
                        return rol.Where(x => x.Length <= 8).ToList();*/ 
                    }

注意:我们不使用OpenID Connect。

我需要(g.MailNickname)的签名用户。我有未经授权的错误。

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要授予应用Directory.Read.All委派权限并获得管理员同意。

找到您在Azure门户上注册的应用程序->单击API权限->选择Microsoft Graph->委托权限->检查Directory.Read.All

enter image description here

请记住单击“授予管理员同意”按钮。

然后更改Additionaloginparams,资源应为https://graph.microsoft.com。有关更多详细信息,请参见this article(您可以集中于配置Web应用以请求访问正确的资源部分)。