.net 获取Azure AD用户在声明中所属的组的列表

.net 获取Azure AD用户在声明中所属的组的列表,.net,azure-active-directory,adal,azure-ad-graph-api,.net,Azure Active Directory,Adal,Azure Ad Graph Api,我正在根据Azure Active Directory对我的web api的用户进行身份验证。 现在,我想获得该用户所属的组的列表 我更改了应用程序清单以包括 "groupMembershipClaims": "All", 但所有这些都是添加声明组,而不是组名 我已为门户中的我的应用向Windows Azure Active Directory授予了所有(8)委派权限。您向应用授予了哪些权限?您需要明确请求读取组的能力(请参阅中的group.read.all)。从今天起,这些权限只能由管理员同

我正在根据Azure Active Directory对我的web api的用户进行身份验证。 现在,我想获得该用户所属的组的列表

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

"groupMembershipClaims": "All",
但所有这些都是添加声明组,而不是组名


我已为门户中的我的应用向Windows Azure Active Directory授予了所有(8)委派权限。

您向应用授予了哪些权限?您需要明确请求读取组的能力(请参阅中的group.read.all)。从今天起,这些权限只能由管理员同意。

我就是这么做的

让我们将我的Azure广告应用程序称为“广告应用程序”

广告应用程序

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

WindowsAzure活动目录

应用程序权限:0

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

清单具有以下设置:

“groupMembershipClaims”:“SecurityGroup”

后端API

以下是我返回用户组的方法。您可以发送用户id,如果没有,则使用claims.id中的id,意思是“objectIdentifier”

公共静态IEnumerable GetGroupMembershipByObjectId(字符串id=null) { if(string.IsNullOrEmpty(id)) id=ClaimsPrincipal.Current.FindFirst(“http://schemas.microsoft.com/identity/claims/objectidentifier1.价值; IList groupMembership=新列表(); 尝试 { ActiveDirectoryClient ActiveDirectoryClient=ActiveDirectoryClient; IUser user=activeDirectoryClient.Users.Where(u=>u.ObjectId==id).ExecuteSingleAsync().Result; var userFetcher=(IUserFetcher)user; IPagedCollection pagedCollection=userFetcher.MemberOf.ExecuteAsync().Result; 做 { List directoryObjects=pagedCollection.CurrentPage.ToList(); foreach(目录对象中的IDirectoryObject目录对象) { 如果(directoryObject是组) { var group=directoryObject作为组; groupMembership.Add(group.DisplayName); } } pagedCollection=pagedCollection.GetNextPageAsync().Result; }while(pagedCollection!=null); } 捕获(例外e) { 异常处理程序.HandleException(e); 投掷e; } 返回集团成员资格; }
我不能告诉你这是否是通过最佳实践来实现的,但它对我来说是有效的。

如果用户有150多个组,则只返回类似于“Graph:link”的calim中指向Graph API的链接,而不返回组。这是出于性能原因。然后需要调用Graph API(MS Graph API-最新,或AD Graph API-旧)迭代所有组。

以下是我们在项目中所做的:

登录并单击Azure Active Directory
App registrations
->
->
清单
,然后将
groupMembershipClaims
设置为
7
。您可以在此处阅读更多信息:

然后,您可以按如下方式访问用户组:

[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,您可以识别AD中的组。

我添加了有关添加权限的信息。这篇文章的任何读者都需要注意:编写的后端API方法不会检索可传递的组成员身份。前一种方法通过AD门户配置来检索。谢谢为我工作:-)如果您在上面的示例中包含必要的名称空间,那就太好了。@Rod-Holy moly,这是很久以前的事了,我不能肯定!但是,我相信这是当时作为Azure Web应用程序运行的WebAPI项目:)希望能有所帮助
ActiveDirectoryClient-ActiveDirectoryClient=ActiveDirectoryClient
此行不起作用。这里有一个大警告:如果用户在5个以上的组中,他们只会得到OP提到的“限制令牌大小”的“hasGroups”声明。意外地遇到这种情况。当用户是5个以上组的一部分时,“hasGroups”声明会被添加,尽管这似乎在任何地方都没有记录。5或更少,您将得到少数具有组的对象id的“组”声明。
[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; }
}
[{"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"}]