1

AADアカウントを使用してMS Graphにアクセスしようとしていますが、このアカウントはグローバル管理者であり、あらゆる権限が委任されています。インタラクティブなログインなしで、つまりUserPasswordCredentialを使用してやりたいアクセストークンの妥当性検査エラーUserCredentialsを使用したMSのグラフ

enter image description here

私の流れ:

は、トークンの取得:MSグラフにアクセスしようとすると、私はエラーを取得する

public async Task<string> GetUserAccessTokenAsync() 
     { 
      UserPasswordCredential userPasswordCredential = new UserPasswordCredential("[email protected]", "password"); 
      AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com"); 
      AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.windows.net", appId, userPasswordCredential); 

      return token.AccessToken; 
     } 

トークン使用:

public static GraphServiceClient GetAuthenticatedClient() 
     { 
      GraphServiceClient graphClient = new GraphServiceClient(
       new DelegateAuthenticationProvider(
        async (requestMessage) => 
        { 
         Adal adal = new Adal(); 
         string accessToken = await adal.GetUserAccessTokenAsync(); 

         // Append the access token to the request. 
         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken); 
        })); 
      return graphClient; 
     } 

を呼び出そうとしイベントを読むためのMSグラフ:

try 
     { 
      // Get events. 
      items = await eventsService.GetMyEvents(graphClient); 
     } 
     catch (ServiceException se) 
     { 
      //this is where I get the error 
     } 

委任アクセス権: enter image description here

任意のアイデア私が間違っているつもりですか?

答えて

2

あなたのリソースURIは少なくとも間違っています。それは次のようになります。

public async Task<string> GetUserAccessTokenAsync() 
{ 
    UserPasswordCredential userPasswordCredential = new UserPasswordCredential("[email protected]", "password"); 
    AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com"); 
    AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", appId, userPasswordCredential); 

    return token.AccessToken; 
} 

https://graph.windows.net/は、AzureのADグラフではなく、MSグラフです。

MS Graph APIでは、https://graph.microsoft.com/を使用する必要があります。

+0

lord ...時には自分自身も驚いています。ありがとうございました。 –

+1

コード例でリソースURIを入力しましたが、修正されました。 – juunas

0

私は管理者権限を取得する必要があると私は理解してより多くの研究をした後。 "App-onlyスコープ(アプリケーションロールとも呼ばれます)は、スコープによって提供される特権の完全なセットをアプリケーションに与えます.App-onlyスコープは、通常、サインインされたユーザーなしでサービスとして実行されるアプリケーションによって使用されます存在しています " https://graph.microsoft.io/en-us/docs/authorization/permission_scopes 委任された権限の下にあったため、ユーザートークンで正常な結果が得られました。 これは将来誰かに役立つことを願っています。我々はアプリケーション専用スコープの代わりに、委任 権限を使用していることを与えられた管理者権限が必要ですように見える、さらなる研究の後

関連する問題