亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取每個應用程序的所有用戶 Azure Active Directory

獲取每個應用程序的所有用戶 Azure Active Directory

C#
小怪獸愛吃肉 2023-07-09 15:17:08
我正在嘗試使用 .NET Web API C# 訪問 Azure Active Directory 中我的應用程序中的用戶。我嘗試從客戶端獲取用戶:await this.aadClient.Users.ExecuteAsync()但它獲取 AD 中的所有用戶,而不是每個應用程序。我嘗試獲取應用程序的成員:var apps = await this.aadClient.Applications.Where(x => x.AppId == this.appId)                                    .Expand(x => x.Members).ExecuteAsync();var app = apps.CurrentPage.FirstOrDefault();var members = app.Members.CurrentPage;但盡管 appId 是正確的并且我的應用程序中有 19 個用戶,但結果始終為空。有人知道可能是什么問題嗎?編輯獲取客戶:var context = new AuthenticationContext($"https://login.microsoftonline.com/[tenant]", false);var aadClient = new ActiveDirectoryClient(                new Uri(new Uri("https://graph.windows.net"), [tenant]),                async () => await context.AcquireTokenAsync("https://graph.windows.net", new ClientCredential([clientId], [clientSecret])));
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

廣告圖客戶端調用廣告圖 api,將用戶分配到您的應用程序的 api 是

https://graph.windows.net/{tenant}/servicePrincipals/{servicePrincipalId}6f/appRoleAssignedTo

所以代碼應該是

aadClient.ServicePrincipals.GetByObjectId("").AppRoleAssignedTo

您可以找到服務 servicePrincipalId,如下所示。它是您的企業應用程序的 ObjectId。

http://img1.sycdn.imooc.com/64aa5f1f0001989306530402.jpg

Directory.Read.All需要許可。點擊應用注冊->找到您的應用程序(與提供clientId的應用程序相同)->API權限

http://img4.sycdn.imooc.com/64aa5f2a000187bd06500590.jpg

請記住單擊Grant admin consent按鈕,因為此權限需要管理員同意。

http://img2.sycdn.imooc.com/64aa5f3a0001c8ea05780245.jpg

更新: 我可以成功地將用戶分配給應用程序,這是測試代碼。


using System;

using System.Threading.Tasks;

using Microsoft.Azure.ActiveDirectory.GraphClient;

using Microsoft.IdentityModel.Clients.ActiveDirectory;


namespace ConsoleApp13

? ? {

? ? ? ? class Program

? ? ? ? {

? ? ? ? ? ? static void Main(string[] args)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Uri servicePointUri = new Uri("https://graph.windows.net");

? ? ? ? ? ? ? ? Uri serviceRoot = new Uri(servicePointUri, "{tenant}");??

? ? ? ? ? ? ? ? var aadClient = new ActiveDirectoryClient(

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? serviceRoot,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?getToken);


? ? ? ? ? ? ? ? var a = aadClient.ServicePrincipals.GetByObjectId("{objectId}").AppRoleAssignedTo.ExecuteAsync().Result;

? ? ? ? ? ? ? ? Console.WriteLine(a.CurrentPage.Count);


? ? ? ? ? ? }


? ? ? ? ? ? public static async Task<string> getToken()

? ? ? ? ? ? {

? ? ? ? ? ? ? ? var context = new AuthenticationContext($"https://login.microsoftonline.com/{tenant}", false);

? ? ? ? ? ? ? ? return context.AcquireTokenAsync("https://graph.windows.net", new ClientCredential("{client_id}", "{client_secret}")).Result.AccessToken;

? ? ? ? ? ? }

? ? ? ? }

? ? }

確保您已授予Directory.Read.All您的應用程序權限。您可以通過解碼令牌在訪問令牌中檢查它。

http://img4.sycdn.imooc.com/64aa5f4d0001d0e505530410.jpg


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號