Lun*_*Dev 5 azure access-token oauth-2.0 azure-active-directory microsoft-graph-api
我正在开发一个 ASP.NET MVC5 Web 应用程序,它使用 Azure ADAL 库对用户进行身份验证,它工作正常,但是,当我手动向图形发送请求时,例如:GET https://graph.microsoft.com/v1.0 /me或 GET https://graph.microsoft.com/v1.0/groups ?$filter=from/displayName eq 'whatever'。
我尝试更新 Azure 中的应用程序注册以添加所需的图形权限,并且我还尝试创建新的应用程序注册,无论我做什么,我的请求将始终响应 401 未经授权,我是否缺少任何内容?
编辑:邮递员的响应示例
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token validation failure.",
"innerError": {
"request-id": "a142576b-acce-4e59-8a8d-adede61aaf59",
"date": "2017-04-05T13:27:36"
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:C# 请求示例
public async Task<GroupGraph> GetGroupIdByDisplayName(string displayName)
{
var accessToken = await authenticationService.GetTokenUserOnly();
GroupGraph groupGraphResponse = null;
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, $"https://graph.microsoft.com/v1.0/groups?$filter=from/displayName eq '{displayName}'"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
using (var response = client.SendAsync(request).Result)
{
if (response.IsSuccessStatusCode)
{
using (var content = response.Content)
{
var result = await content.ReadAsStringAsync();
groupGraphResponse = JsonConvert.DeserializeObject<GroupGraph>(result);
}
}
}
}
}
return groupGraphResponse;
}
Run Code Online (Sandbox Code Playgroud)
编辑:我获取令牌的方式
public async Task<string> GetTokenUserOnly()
{
string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(clientId, appKey);
// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new TableTokenCache(signedInUserID));
//AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(graphResourceID, clientcred);
return authenticationResult.AccessToken;
}
Run Code Online (Sandbox Code Playgroud)
您无法使用 ADAL 获取图表的令牌。微软.com。ADAL 用于图表。窗口.net。
为了获取 Graph 库 (graph.windows.com) 的令牌,请查看 Nuget 包 Microsoft.Graph。Microsoft 还提供了一些有关如何使用 Graph 提取用户信息的文档。
但请预先警告,并行使用图形库和 ADAL 库可能会导致一些奇怪的副作用,例如凭证缓存被清除。
| 归档时间: |
|
| 查看次数: |
11416 次 |
| 最近记录: |