Error handling Microsoft Graph:当前经过身份验证的上下文对此请求无效

Error handling Microsoft Graph:当前经过身份验证的上下文对此请求无效,error-handling,azure-active-directory,microsoft-graph-api,adal,Error Handling,Azure Active Directory,Microsoft Graph Api,Adal,我有一个应用程序,它使用MSAL和v2.0端点登录用户并获取令牌 我最近将其更改为ADAL和普通AAD端点(也更改了应用程序),现在当我尝试使用GraphService时,出现以下错误:当前已验证的上下文对此请求无效 我的用户是管理员 所有权限都已委派 已成功检索令牌 以下是我使用的代码: public static GraphServiceClient GetAuthenticatedClient() { GraphServiceClient gra

我有一个应用程序,它使用MSAL和v2.0端点登录用户并获取令牌

我最近将其更改为ADAL和普通AAD端点(也更改了应用程序),现在当我尝试使用GraphService时,出现以下错误:
当前已验证的上下文对此请求无效

  • 我的用户是管理员
  • 所有权限都已委派
  • 已成功检索令牌
以下是我使用的代码:

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;
        }
调用方法,实际错误发生在:

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

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

            }
获取令牌:

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;
        }
公共异步任务GetTokenAsync() { ClientCredential cc=新的ClientCredential(appId,appSecret); AuthenticationContext authContext=新的AuthenticationContext(“https://login.microsoftonline.com/tenant.onmicrosoft.com"); AuthenticationResult=等待authContext.AcquireTokenAsync(“https://graph.microsoft.com“,cc); 返回result.AccessToken; } 在网上找不到任何内容,因此我不确定如何继续

错误:

此异常是由使用客户端凭据流获取的令牌引起的。在此流中,没有Me的上下文

要解决此问题,您需要指定要获取其事件的名称。或者您需要提供委托令牌

代码供您参考:

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

我认为“送信人”应该是“送信人”。我不确定它是否不区分大小写。@RasmusW不应该区分大小写,因为它使用带有“bearer”的MSAL工作。请共享包含请求id和时间戳的错误正文,以帮助调试。您可以共享实际触发错误的代码吗?@SriramDhanasekaran MSFT我为问题添加了更多信息。我认为这可能源于我获取令牌的位置,使用ClienCredential获取登录用户的令牌感觉不正确?如何提供代理令牌?代理令牌需要用户交互才能输入用户名和密码。例如,我们可以使用**授权代码授权流**来获取委托令牌(参见此流)。