我正在針對Azure Active Directory對我的web api的用戶進行身份驗證。 現在我想獲得該用戶所屬的組的列表。獲取Azure AD用戶所屬的組列表中的聲明
我改變了應用程序清單包括
"groupMembershipClaims": "All",
但所有這確實是增加要求hasGroups但沒有組名。
我在門戶網站中爲我的應用授予了Windows Azure Active Directory的全部(8)委託權限。
我正在針對Azure Active Directory對我的web api的用戶進行身份驗證。 現在我想獲得該用戶所屬的組的列表。獲取Azure AD用戶所屬的組列表中的聲明
我改變了應用程序清單包括
"groupMembershipClaims": "All",
但所有這確實是增加要求hasGroups但沒有組名。
我在門戶網站中爲我的應用授予了Windows Azure Active Directory的全部(8)委託權限。
您授予您的應用程序的權限是什麼?您需要明確請求讀取組的能力(請參閱https://msdn.microsoft.com/en-us/library/azure/ad/graph/howto/azure-ad-graph-api-permission-scopes中的group.read.all)。截至今天,這些權限只能由管理員同意。
我已經完成了這個。
讓我們打電話給我的Azure AD Appication「AD-App」。
AD-應用
權限到其它應用程序被設置爲;
Windows Azure Active Directory。
應用權限:0.
委託權限2( 「讀取目錄數據」, 「登錄和讀的用戶簡檔」
清單具有以下設置:
「groupMembershipClaims」:「SecurityGroup」
後端API
以下是我返回用戶組的方法。您可以發送用戶標識,如果沒有,則使用來自聲明的標識。 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;
}
我不能告訴你這是通過最佳實踐或不是,但它適用於我。
如果用戶擁有超過150個組,則只返回calim中Graph API的鏈接,如「graph:link」,而不是組。這是出於性能原因完成的。然後您需要調用Graph API(MS Graph API - 最新版本或AD Graph API - 較舊版本)來遍歷所有組。
我添加了有關添加的權限的信息 – jlp