![](/img/trans.png)
[英]How to get the Azure AD User Group Name from User Name (Identity Name) in C# MVC .net core?
[英]C# - Get group info from Azure AD
我正在使用 Azure AD 登錄來讓應用程序使用 Microsoft 憑據登錄並獲取用戶所屬的組(及其信息)。
通過這一行,我可以獲得登錄用戶: var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity;
通過這一行,我可以獲得用戶所屬的組: var groups = userClaims?.Claims.Where(claim => claim.Type == "groups").Select(c => c.Value);
現在我正在嘗試集成 Microsoft.Graph,以便獲取組的信息(我只需要組的顯示名稱)。
我正在使用這段代碼:
GraphServiceClient graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("Bearer");
return Task.FromResult(0);}));
var group = graphClient.Groups[g].Request().GetAsync();
這不起作用,或者我似乎無法弄清楚如何讀取組 var 的屬性。
有什么簡單的方法可以獲取組的信息嗎?
根據官方ms 圖,您可以通過以下方式獲取組顯示名稱
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var groups = await graphClient.Groups
.Request()
.GetAsync();
另一個解決方法如下:
var graphClient = GraphUtility.CreateGraphClient(accessToken);
var targetGroupCollection = await graphClient.Groups.Request()
.Filter($"startsWith(displayName,'{groupName}')")
.GetAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.