7

我正在针对 Azure Active Directory 对我的 Web api 的用户进行身份验证。现在我想获取该用户所属的组列表。

我更改了应用程序清单以包括

"groupMembershipClaims": "All",

但这所做的只是添加声明 hasGroups 但没有组名。

我在门户中为我的应用程序授予了 Windows Azure Active Directory 的所有 (8) 委托权限。

4

4 回答 4

9

我确实做到了这一点。

我们将我的 Azure AD 应用程序称为“AD-App”。

广告应用

对其他应用程序的权限设置为;

Windows Azure 活动目录。

申请权限:0。

委派权限 2(“读取目录数据”、“登录并读取用户配置文件”。

清单具有以下设置:

“groupMembershipClaims”:“安全组”

后端 API

以下是我返回用户组的方法。要么您发送用户 ID,否则它使用声明中的 ID。Id 的意思是“objectIdentifier”。

        public static IEnumerable<string> GetGroupMembershipsByObjectId(string id = null)
    {
        if (string.IsNullOrEmpty(id))
            id = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

        IList<string> groupMembership = new List<string>();
        try
        {
            ActiveDirectoryClient activeDirectoryClient = ActiveDirectoryClient;
            IUser user = activeDirectoryClient.Users.Where(u => u.ObjectId == id).ExecuteSingleAsync().Result;
            var userFetcher = (IUserFetcher)user;

            IPagedCollection<IDirectoryObject> pagedCollection = userFetcher.MemberOf.ExecuteAsync().Result;
            do
            {
                List<IDirectoryObject> directoryObjects = pagedCollection.CurrentPage.ToList();
                foreach (IDirectoryObject directoryObject in directoryObjects)
                {
                    if (directoryObject is Group)
                    {
                        var group = directoryObject as Group;
                        groupMembership.Add(group.DisplayName);
                    }
                }
                pagedCollection = pagedCollection.GetNextPageAsync().Result;
            } while (pagedCollection != null);

        }
        catch (Exception e)
        {
            ExceptionHandler.HandleException(e);
            throw e;
        }

        return groupMembership;
    }

我不能告诉你这是否是通过最佳实践完成的,但它对我有用。

于 2016-02-22T12:14:40.573 回答
3

这是我们在一个项目中所做的:

登录到https://portal.azure.com并单击Azure Active Directory-> App registrations-> <YOUR_APP>->Manifest并设置groupMembershipClaims7. 您可以在此处阅读有关此内容的更多信息:

https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-application-manifest

然后,您可以像这样访问用户组:

[Route("api/[controller]")]
[ApiController]
public class CurrentUserController : ControllerBase
{
    [HttpGet("groups")]
    [ProducesResponseType(typeof(IEnumerable<ClaimsViewModel>), (int)HttpStatusCode.OK)]
    public IActionResult Groups()
    {
        return Ok(User.Claims.Where(claim => claim.Type == "groups").Select(c => new ClaimsViewModel() { Type = c.Type, Value = c.Value }));
    }
}

public class ClaimsViewModel
{
    public string Type { get; set; }
    public string Value { get; set; }
}

带有假对象 ID 的示例响应:

[{"type":"groups","value":"12fef9e0-4b73-425d-91b7-30c027aa4945"},{"type":"groups","value":"12fef9e0-4b73-425d-91b7-30c027aa4946"},{"type":"groups","value":"12fef9e0-4b73-425d-91b7-30c027aa4947"},{"type":"groups","value":"12fef9e0-4b73-425d-91b7-30c027aa4948"},{"type":"groups","value":"12fef9e0-4b73-425d-91b7-30c027aa4949"}]

有了这些 ID,您就可以识别 AD 中的组。

于 2018-06-13T11:46:29.750 回答
1

如果用户有超过 150 个组,则仅返回到 Graph API 的链接,如“graph:link”,而不是组。这样做是出于性能原因。然后,您需要调用 Graph API(MS Graph API - 最新,或 AD Graph API - 较旧)来遍历所有组。

于 2017-10-31T17:48:53.490 回答
0

您为您的应用授予了哪些权限?您需要明确请求读取组的能力(请参阅https://msdn.microsoft.com/en-us/library/azure/ad/graph/howto/azure-ad-graph-api-中的 group.read.all权限范围)。截至今天,这些权限只能由管理员同意。

于 2016-02-11T19:39:58.297 回答