获取每个应用程序的所有用户 Azure Active Directory

我正在尝试使用 .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])));


小怪兽爱吃肉
浏览 70回答 1
1回答

慕容森

广告图客户端调用广告图 api,将用户分配到您的应用程序的 api 是https://graph.windows.net/{tenant}/servicePrincipals/{servicePrincipalId}6f/appRoleAssignedTo所以代码应该是aadClient.ServicePrincipals.GetByObjectId("").AppRoleAssignedTo您可以找到服务 servicePrincipalId,如下所示。它是您的企业应用程序的 ObjectId。Directory.Read.All需要许可。点击应用注册->找到您的应用程序(与提供clientId的应用程序相同)->API权限请记住单击Grant admin consent按钮,因为此权限需要管理员同意。更新: 我可以成功地将用户分配给应用程序,这是测试代码。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您的应用程序权限。您可以通过解码令牌在访问令牌中检查它。
打开App,查看更多内容
随时随地看视频慕课网APP