【发布时间】:2017-06-24 17:07:25
【问题描述】:
我有一个使用 MSAL 和 v2.0 端点登录用户并获取令牌的应用。
我最近将其更改为 ADAL 和正常的 AAD 端点(也更改了应用程序),现在当我尝试使用 GraphService 时出现以下错误:Current authenticated context is not valid for this request
- 我的用户是管理员
- 已委派所有权限
- 成功获取令牌
这是我使用的代码:
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;
}
在网上找不到任何东西,所以我不知道如何继续。
【问题讨论】:
-
我认为“承载者”需要是“承载者”。我不确定它是否不区分大小写。
-
@RasmusW 不应该,因为它可以将 MSAL 与“bearer”一起使用
-
请分享包含request-id和时间戳的错误正文以帮助调试。
-
你能分享实际触发错误的代码吗?
-
@SriramDhanasekaran-MSFT 我在问题中添加了更多信息。我认为这可能源于我获取令牌的位置,使用 ClienCredential 获取已登录用户的令牌感觉不对?
标签: error-handling azure-active-directory microsoft-graph-api adal