Я взял ссылку из этого проекта git. В этом проекте есть код для подключения и получения информации о профиле пользователя.
Во время запуска проекта я увидел, что не могу получить успешный результат из приведенного ниже кода:
result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential,
new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
Вот полный код-
// Get the access token from the cache
string userObjectID =
ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")
.Value;
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
//AuthenticationContext authContext = new AuthenticationContext(authority,
// new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, appKey);
AuthenticationContext authContext = new AuthenticationContext(authority, true);
// AcquireTokenSilentAsync
result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential,
new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
// Call the Graph API manually and retrieve the user's 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", result.AccessToken);
HttpResponseMessage response = await client.SendAsync(request);
// Return the user's profile in the view.
if (response.IsSuccessStatusCode)
{
string responseString = await response.Content.ReadAsStringAsync();
profile = JsonConvert.DeserializeObject<UserProfile>(responseString);
}
Я устал заменять UserIdentifierType.UniqueId на UserIdentifierType.RequiredDisplayableId, но так и не смог получить успешный результат.
Пробуя весь день, я не могу понять это решение.
Я очень ценю любую помощь.
[email protected] graphDem0
), представленной в этом примере кода, без изменения кода. А если вы хотите использовать собственный клиент, следуйте инструкции как запустить этот пример кода. - person Fei Xue - MSFT   schedule 12.01.2017