Graph API:通过.net SDK在没有用户的情况下访问

时间:2019-01-29 05:25:54

标签: c# azure-ad-graph-api

使用用户身份验证访问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;
        }

0 个答案:

没有答案