gtr*_*edi 15 c# azure-active-directory microsoft-graph-api
我正在尝试创建一个 C# 控制台应用程序来连接到图形 API 并从租户中获取来自 AzureAD 的用户列表。我已经注册了应用程序,管理员给了我以下内容
使用 sdk,我需要使用的 C# 代码如下所示(https://docs.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();
Run Code Online (Sandbox Code Playgroud)
但是,控制台应用程序将作为批处理运行,因此根本没有用户交互。因此,为了提供 authProvider,我在 MS 文档站点上关注了这篇文章:https ://docs.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS
而且我认为出于我的目的,我需要使用“客户端凭据 OAuth 流程”。该 URL 上显示的代码。但这里也是。
IConfidentialClientApplication clientApplication = ClientCredentialProvider.CreateClientApplication(clientId, clientCredential);
ClientCredentialProvider authProvider = new ClientCredentialProvider(clientApplication);
Run Code Online (Sandbox Code Playgroud)
问题是 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;
Run Code Online (Sandbox Code Playgroud)
我对 GitHub 存储库不是很有经验,我使用的是 Visual Studio 2015。我会对示例代码感兴趣;我已经看过了,但找不到任何。MS 有一些讲座,但他们使用另一种类型的身份验证提供程序,它以交互方式进行身份验证,这不是我正在寻找的。我想使用 TenantId/ClientId 和 Client Secret 获取令牌。
Phi*_*ret 35
ClientCredentialProvider是 Microsoft.Graph.Auth 包的一部分。您可以在https://github.com/microsoftgraph/msgraph-sdk-dotnet-auth阅读有关此包的更多信息
请注意,此软件包当前(截至 2019 年 5 月 15 日)处于预览状态,因此您可能需要等待,然后才能在生产应用程序中使用它。
或者,以下示例直接使用适用于 .NET的Microsoft 身份验证库(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();
Run Code Online (Sandbox Code Playgroud)
(请注意,此示例使用最新版本的 Microsoft.Identity.Client 包。早期版本(版本 3 之前)不包括ConfidentialClientApplicationBuilder。)
| 归档时间: |
|
| 查看次数: |
23433 次 |
| 最近记录: |