Microsoft Graph: Current authenticated context is not valid for this request

扶醉桌前 提交于 2019-12-10 10:42:16

问题


I had an app that used MSAL and the v2.0 endpoint to sign in users and get token.

I recently changed it to ADAL and the normal AAD endpoint (also changing the app), and now when I try to use the GraphService I get the following error: Current authenticated context is not valid for this request

  • My user is admin
  • All permissions have been delegated
  • The token is successfully retrieved

Here is the code I use:

public static GraphServiceClient GetAuthenticatedClient()
        {
            GraphServiceClient graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        string accessToken = await SampleAuthProvider.Instance.GetUserAccessTokenAsync();

                        // Append the access token to the request.
                        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                    }));
            return graphClient;
        }

Calling the method, where the actual error happens:

try
            {
                // Initialize the GraphServiceClient.
                GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

                // Get events.
                items = await eventsService.GetMyEvents(graphClient);
            }
            catch (ServiceException se)
            {

            }

Getting the token:

public async Task<string> GetTokenAsync()
        {
            ClientCredential cc = new ClientCredential(appId, appSecret);
            AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com");
            AuthenticationResult result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", cc);

            return result.AccessToken;
        }

Can't find anything on this online so I am not sure how to continue.

Error:


回答1:


This exception is caused by the token acquired using the client credentials flow. In this flow, there is no context for Me.

To fix this issue, you need to specify the whose event you want to get. Or you need to provide the delegate-token.

code for your reference:

//var envens=await graphClient.Me.Events.Request().GetAsync();
var envens = await graphClient.Users["xxx@xxx.onmicrosoft.com"].Events.Request().GetAsync();


来源:https://stackoverflow.com/questions/42090125/microsoft-graph-current-authenticated-context-is-not-valid-for-this-request

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