1

我已经实现了在角4.After用户使用Azure的Active Directory的多租户应用程序登录到我的应用我能够得到用户的info.But用户的照片是没有从Active Directory中获得,因为我实现了Graph API,如下面的代码片段所示。获得401(未授权)错误

public Task<UserDto> getPhoto(TenantDto tenantDto) 
    { 
     var client = new HttpClient(); 
     client.BaseAddress = new Uri(String.Format("https://graph.windows.net/{0}/users/{1}/thumbnailPhoto?api-version=1.6", tenantDto.tenantKey, tenantDto.email)); 
     client.DefaultRequestHeaders.Accept.Clear(); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/jpeg")); 
     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tenantDto.token); 
     HttpResponseMessage response = client.GetAsync("").Result; 
     if (response.IsSuccessStatusCode) 
     { 
      return null; 
      //Status status = response.Content.ReadAsAsync<Status>().Result; 
      //if (status.Code == 200) 
      // InBoundResponse = JsonConvert.DeserializeObject<InBoundCallResponse>(status.Data.ToString()); 
      //return InBoundResponse; 
     } 
     else 
     { 
      return null; 
     } 
    } 

这里tenantDto.token不过是一个登录的用户“令牌”虽然调用此图形API我得到401 (Unauthorized)错误。我尝试了所有,但没有用。 我已经改变了图形API设定S1在Active Directory应用程序也喜欢下面附件 enter image description here

另外我有下面的代码它的工作只针对单租户

[Route("AdUserImage"), HttpGet] 
    public async Task<HttpResponseMessage> userImage() 
    { 
     var authContext = new AuthenticationContext("https://login.windows.net/sampletest.onmicrosoft.com/oauth2/token"); 
     var credential = new ClientCredential(clientID, clientSecret); 
     ActiveDirectoryClient directoryClient = new ActiveDirectoryClient(serviceRoot, async() => 
     { 
      var result = await authContext.AcquireTokenAsync("https://graph.windows.net/", credential); 
      return result.AccessToken; 
     }); 

     var user = await directoryClient.Users.Where(x => x.UserPrincipalName == "[email protected]").ExecuteSingleAsync(); 
     DataServiceStreamResponse photo = await user.ThumbnailPhoto.DownloadAsync(); 
     using (MemoryStream s = new MemoryStream()) 
     { 
      photo.Stream.CopyTo(s); 
      var encodedImage = Convert.ToBase64String(s.ToArray()); 
     } 
     //string token = await HttpAppAuthenticationAsync(); 
     Status status = new Status("OK"); 
     status = new Status("Found", null, "User exists."); 

     return Request.CreateResponse(HttpStatusCode.OK, status, _jsonMediaTypeFormatter); 
    } 

试图像,但我需要实现多租户应用程序。

知道的任何答案。提前

感谢........!

+0

有几种不同的'401'错误(参考[这里](https://msdn.microsoft。 COM/EN-US /库/蔚蓝/广告/图/ HOWTO/Azure的广告图-API的错误代码,和错误处理))关于什么的'401'详细的错误信息? –

+0

{ “odata.error”:{ “代码”: “Authentication_MissingOrMalformed”, “消息”:{ “郎”: “EN”, “值”: “访问令牌缺失或残缺。” }, “日期”: “2017-10-11T07:02:15”, “的requestId”: “4f112369-931d-4a24-8f61-e303b0acb9c1”, “值”:空 } }而错误消息调用Graph API,但令牌是有效的,因为在用户直接登录我的应用程序后,我调用了Graph API。谢谢 –

+0

您为访问令牌,委托令牌或应用程序令牌获取哪些流程? –

回答

1

委托用户的令牌:

1 .Acquire经由隐流令牌:

https://login.microsoftonline.com/{tenant}/oauth2/authorize?response_type=token&client_id={clientId}&redirect_uri={redirect_uri}&resource=https%3A%2F%2Fgraph.windows.net&nonce={nonce} 

2 .CALL天青AD格拉夫

GET: https://graph.windows.net/{tenant}/me/thumbnailPhoto?api-version=1.6 
Content-Type: image/jpeg 

应用令牌:

1 .Acquire通过客户端凭证令牌流

POST:https://login.microsoftonline.com/{tenant}/oauth2/token 
grant_type=client_credentials&client_id={client_id}&client_secret={client_secret}&resource=https%3A%2F%2Fgraph.windows.net 

2 .CALL在Azure AD图形

GET:https://graph.windows.net/{tenant}/users/{upn}/thumbnailPhoto?api-version=1.6 
Content-Type: image/jpeg 

如果你只拿到的照片缩略图登录用户的多个租户,您应该登录,与Azure的AD第一,并获得了代表用户的访问令牌,并使用该令牌调用Azure的AD图REST。这两种令牌的区别,您可以参考以下链接:

Get access on behalf of a user

Get access without a user

+0

我无法获取委托用户令牌的令牌以及应用程序令牌 –

+0

应用程序令牌通常用于应用程序注册的单个租户。如果您想获取访问令牌,则可以使用该应用程序用于其他租户,该应用程序的管理员应该在应用程序可以成功获取访问令牌之前授予许可权(“Directory.Read.All”必需的管理员权限)。之后,您还需要从登录用户那里获取租户信息,并在请求中对其进行修改,之后它应该也能正常工作。如果您仍然遇到问题,请分享您获得的结果。 –

+0

@BalarajuPolaki你还有这个问题吗?请随时让我知道,如果有任何步骤阻止你。 –