authContext.AcquireTokenSilentAsync throwing error

放肆的年华 提交于 2019-12-08 02:15:39

问题


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.

来源:https://stackoverflow.com/questions/41586897/authcontext-acquiretokensilentasync-throwing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!