authContext.AcquireTokenSilentAsync throwing error

2019-05-27 07:59发布

I have taken reference from this git project. This project has code to connect and get information about User Profile.

While running the project i saw i am not able to get successful result from below code-

                result = await authContext.AcquireTokenSilentAsync(graphResourceId, credential,
                new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

Here is full code-

 // 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);
            }

I tired replacing UserIdentifierType.UniqueId with UserIdentifierType.RequiredDisplayableId but still not able to get successful result.

After trying all day long, i am not able to figure it out the solution of this.

I really appreciate any help.

0条回答
登录 后发表回答