[英]How to fetch all users from Azure Active Directory Group using C#
我们在 ASP.NET MVC 中开发了一个应用程序。 我们还在 Azure 中配置了 Acitve 目录,其中包含一些组。 要求是,我们需要从 Azure Active Directory 的组中获取所有用户并添加一个新用户。
我们正在使用下面的代码,我猜它要求额外的身份验证。 我们希望在它自己的代码中提供所有身份验证,而不需要弹出窗口进行身份验证。 你能帮忙吗
// Build a client application.
IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create("clientID")
.WithTenantId("tenantId")
.Build();
// Create an authentication provider by passing in a client application and graph scopes.
DeviceCodeProvider authProvider = new DeviceCodeProvider(publicClientApplication, new[] { "User.Read" });
// Create a new instance of GraphServiceClient with the authentication provider.
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var members = await graphClient.Groups["groupId"].Members
.Request()
.GetAsync();
上面的代码显示一条消息“要登录,请使用 web 浏览器打开页面https://microsoft.com/devicelogin并输入代码 G9277ULC9 进行身份验证。”
我们如何在代码本身中提供所有身份验证信息以避免此步骤?
更新的 API 权限如下 -
您可以使用Microsoft Graph SDK来做到这一点。
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var members = await graphClient.Groups["{id}"].Members
.Request()
.GetAsync();
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var directoryObject = new DirectoryObject
{
AdditionalData = new Dictionary<string, object>()
{
{"@odata.id","https://graph.microsoft.com/v1.0/directoryObjects/{id}"}
}
};
await graphClient.Groups["{id}"].Members.References
.Request()
.AddAsync(directoryObject);
更新:
如果您想要一种非交互方式,您需要使用客户端凭据流,即创建authProvider
实例作为客户端凭据提供程序。
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantID)
.WithClientSecret(clientSecret)
.Build();
ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.