0

私はazure ad graph APIを使用して、アクティブなディレクトリからユーザープロファイルデータを取得しています。すべての入力パラメータが正しいですし、トークンはまた、以下のコードで生成されます。しかし、それはresponse.responseとしてユーザプロファイルオブジェクトを与えていません.IsSuccessStatusCodeは常にfalseです。私のミスは何ですか?azure ad graph apiがユーザー情報を取得しない

private readonly string graphUserUrl = "https://graph.windows.net/{0}/me?api-version=1.6" 
    string tenantName = "Microsoft.OnMicrosoft.com"; 
       string authString = "https://login.microsoftonline.com/" + tenantName; 
       AuthenticationContext authenticationContext = new AuthenticationContext(authString, false); 
       // Config for OAuth client credentials    
       ClientCredential clientCred = new ClientCredential(clientId, appKey); 
       string resource = "https://graph.windows.net"; 
       string token = ""; 
       try 
       { 
        AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred); 
        token = authenticationResult.AccessToken; 
       } 
       catch (AuthenticationException ex) 
       { 

       } 

       UserProfile profile; 
       string requestUrl = String.Format(CultureInfo.InvariantCulture,graphUserUrl,HttpUtility.UrlEncode(tenantId)); 
       HttpClient client = new HttpClient(); 
       HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); 
       request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); 
       //HttpResponseMessage response = await client.SendAsync(request); 
       HttpResponseMessage response = client.SendAsync(request).Result; 

       // Return the user's profile in the view. 
       if (response.IsSuccessStatusCode) 
       { 
        string responseString = await response.Content.ReadAsStringAsync(); 
        profile = JsonConvert.DeserializeObject<UserProfile> 
(responseString); 
       } 
+0

あなたがこの問題を修正する必要がありましたか?まだ問題がある場合は、私に知らせてください。 –

答えて

3

ユーザートークンを取得するためにアプリケーショントークンを使用していました。このような種類のサインインユーザー情報がトークンに存在しないため、エラーが発生します。アプリケーショントークンを使用してユーザー情報を読み取るには、我々は以下のreuqestのようなusers\{id | userPrincipalName}を使用してmeを交換する必要があります。

https://graph.windows.net/{tenant}/users/{id|userPrincipalName}?api-version=1.6 

アプリケーショントークンは通常クライアントの資格流れで使用して取得デーモンサービスで使用されています。 このフローの詳細は、hereを参照してください。

me keyworldを使用する場合は、the OAuth 2 code grant flowのように取得できるdelegate-tokenを使用する必要があります。 previews threadに基づいて、あなたはウェブアプリを使って開発していたようです。プロファイルを表示するにはAzure AD Graphでコードサンプルhereを確認してください。ここでトークンを取得するための相対的なコードは次のとおりです。

string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value; 
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID)); 
ClientCredential credential = new ClientCredential(clientId, appKey); 
result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 

そしてここでは、Azureの広告のAUTHENCATIONのsecnarioに関する有用な文書である:

Authentication Scenarios for Azure AD

+0

もう少し一般的に言うと、* app *のトークンを取得するときにクライアントの資格情報を使用し、ログインしたユーザーは問題になりません。また、あなたのアプリのユーザがAzure ADで十分な特権を持っていないのにあなたのアプリを使って部分的なアクセスを許可したい場合にも使用できます。 – juunas

関連する問題