使用用户身份验证访问Graph API没问题
var user = new PublicClientApplication("---");
var userClient = new GraphServiceClient(
"https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", await GetTokenAsync(user));
}));
static async Task<string> GetTokenAsync(IPublicClientApplication user)
{
string[] scopes = { "User.Read", "Directory.Read.All" };
var authResult = await user.AcquireTokenAsync(scopes);
return authResult.AccessToken;
}
但是,与Application相似的代码也需要用户验证。
此链接(https://docs.microsoft.com/en-us/graph/auth-v2-service)描述了如何使用HTTP客户端执行主题。我该如何仅使用.net SDK?
var app = new ConfidentialClientApplication("---", @"https://login.microsoftonline.com/common",
"http://localhost", new ClientCredential("---"), new TokenCache(), new TokenCache());
public static async Task<string> GetTokenForAppAsync(IConfidentialClientApplication identityAppOnlyApp)
{
var authResult = await identityAppOnlyApp.AcquireTokenForClientAsync(new[] { "https://graph.microsoft.com/.default" });
return authResult.AccessToken;
}