【发布时间】:2017-06-25 22:11:07
【问题描述】:
我正在尝试使用 AAD 帐户访问 MS Graph,该帐户是全局管理员,并且拥有所有权限。我想在没有交互式登录的情况下执行此操作,即使用UserPasswordCredential。尝试访问 MS Graph 时出现错误:
我的流程:
获取令牌:
public async Task<string> GetUserAccessTokenAsync()
{
UserPasswordCredential userPasswordCredential = new UserPasswordCredential("user@tenant.onmicrosoft.com", "password");
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com");
AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.windows.net", appId, userPasswordCredential);
return token.AccessToken;
}
使用令牌:
public static GraphServiceClient GetAuthenticatedClient()
{
GraphServiceClient graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
Adal adal = new Adal();
string accessToken = await adal.GetUserAccessTokenAsync();
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
return graphClient;
}
尝试调用 MS Graph 读取事件:
try
{
// Get events.
items = await eventsService.GetMyEvents(graphClient);
}
catch (ServiceException se)
{
//this is where I get the error
}
任何想法我哪里出错了?
【问题讨论】:
标签: .net authentication azure-active-directory microsoft-graph-api adal