问题描述
我正在针对 Azure Active Directory 对我的 Web api 用户进行身份验证.现在我想获取该用户所属的组列表.
I am authenticating users of my web api against Azure Active Directory. Now I want to get a list of groups that this user belongs.
我更改了应用程序清单以包含
I changed application manifest to include
"groupMembershipClaims": "All",
但这所做的只是添加声明 hasGroups 但没有组名.
but all this does is to add claim hasGroups but no group names.
我在门户中为我的应用授予了 Windows Azure Active Directory 的所有 (8) 项委派权限.
I granted all (8) Delegated Permissions to Windows Azure Active Directory for my app in the portal.
推荐答案
我确实做到了.
让我们将我的 Azure AD 应用程序称为AD-App".
Let's call my Azure AD appication "AD-App".
广告应用
其他应用程序的权限设置为;
Windows Azure 活动目录.
Windows Azure Active Directory.
应用程序权限:0.
委派权限 2(读取目录数据"、登录并读取用户配置文件".
Delegated Permissions 2 ("Read directory data", "Sign in and read user profile".
Manifest 有如下设置:
"groupMembershipClaims": "SecurityGroup"
"groupMembershipClaims": "SecurityGroup"
后端 API
以下是我返回用户组的方法.要么您发送用户 ID,否则它使用声明中的 ID.Id 的意思是objectIdentifier".
The following is my method to return the users groups. Either you send in the users id, if not it uses the id from claims. Id meaning "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;
}
我不能告诉你这是否是最佳实践,但它对我有用.
I can't tell you wether this is done by best practice or not, but it works for me.
这篇关于在声明中获取 Azure AD 用户所属的组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)