我正在尝试创建一个 C# 控制台应用程序来连接到图形 API 并从租户处获取 AzureAD 的用户列表。我已经注册了该应用程序,管理员给了我以下内容
- 租户名称和租户 ID
- 客户端 ID(有时也称为应用 ID)
- 客户端 secret
使用 SDK,我需要使用的 C# 代码如下所示 ( https://learn.microsoft.com/en-us/graph/api/user-list?view=graph-rest-1.0&tabs=cs ):
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var users = await graphClient.Users
.Request()
.GetAsync();
但是,控制台应用程序将作为批处理进程运行,因此根本不会有用户交互。因此,为了提供 authProvider,我按照 MS 文档网站上的这篇文章进行操作:https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS
我认为出于我的目的,我需要选择“客户端凭据 OAuth 流程”。该 URL 上显示的代码。但这里也是如此。
IConfidentialClientApplication clientApplication = ClientCredentialProvider.CreateClientApplication(clientId, clientCredential);
ClientCredentialProvider authProvider = new ClientCredentialProvider(clientApplication);
问题是 Visual Studio 无法识别 ClientCredentialProvider 类。我不确定要导入哪个程序集。我在顶部使用以下用法。
using Microsoft.Identity.Client;
using Microsoft.IdentityModel.Clients;
using Microsoft.IdentityModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
我对 GitHub 存储库不太有经验,并且我正在使用 Visual Studio 2015。我对示例代码感兴趣;我看过但找不到。 MS 有一些讲座,但他们使用另一种类型的身份验证提供程序,该提供程序以交互方式进行身份验证,这不是我想要的。我想使用 TenantId/ClientId 和 Client Secret 获取 token 。
最佳答案
ClientCredentialProvider 是 Microsoft.Graph.Auth 包的一部分。您可以在 https://github.com/microsoftgraph/msgraph-sdk-dotnet-auth 阅读有关此软件包的更多信息。
请注意,此软件包目前(截至 2019 年 5 月 15 日)处于预览状态,因此您可能需要等待,然后再在生产应用程序中使用它。
或者,以下示例使用 Microsoft Authentication Library for .NET (MSAL) 直接使用仅应用身份验证设置 Microsoft Graph SDK:
// The Azure AD tenant ID or a verified domain (e.g. contoso.onmicrosoft.com)
var tenantId = "{tenant-id-or-domain-name}";
// The client ID of the app registered in Azure AD
var clientId = "{client-id}";
// *Never* include client secrets in source code!
var clientSecret = await GetClientSecretFromKeyVault(); // Or some other secure place.
// The app registration should be configured to require access to permissions
// sufficient for the Microsoft Graph API calls the app will be making, and
// those permissions should be granted by a tenant administrator.
var scopes = new string[] { "https://graph.microsoft.com/.default" };
// Configure the MSAL client as a confidential client
var confidentialClient = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithAuthority($"https://login.microsoftonline.com/$tenantId/v2.0")
.WithClientSecret(clientSecret)
.Build();
// Build the Microsoft Graph client. As the authentication provider, set an async lambda
// which uses the MSAL client to obtain an app-only access token to Microsoft Graph,
// and inserts this access token in the Authorization header of each API request.
GraphServiceClient graphServiceClient =
new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {
// Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
var authResult = await confidentialClient
.AcquireTokenForClient(scopes)
.ExecuteAsync();
// Add the access token in the Authorization header of the API request.
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
})
);
// Make a Microsoft Graph API query
var users = await graphServiceClient.Users.Request().GetAsync();
(请注意,此示例使用最新版本的 Microsoft.Identity.Client 包。早期版本(版本 3 之前)不包含 ConfidentialClientApplicationBuilder。)
关于c# - 将 authProvider 与 MS SDK 结合使用以在 C# 中进行图形调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152979/