Azure active directory 令牌缓存:在缓存Azure AD Api中未找到匹配的令牌
我想使用Azure AD Api,但由于某些原因无法获取令牌。我有两种方法,打电话后得到:Azure active directory 令牌缓存:在缓存Azure AD Api中未找到匹配的令牌,azure-active-directory,access-token,Azure Active Directory,Access Token,我想使用Azure AD Api,但由于某些原因无法获取令牌。我有两种方法,打电话后得到: TokenCache: No matching token was found in the cache iisexpress.exe Information: 0 这是我的密码: public string GetToken() { string authority = "https://login.microsoftonline.com/{tenant
TokenCache: No matching token was found in the cache iisexpress.exe Information: 0
这是我的密码:
public string GetToken()
{
string authority = "https://login.microsoftonline.com/{tenantId}/";
string clientId = "";
string secret = "";
string resource = "https://graph.windows.net/";
var credential = new ClientCredential(clientId, secret);
AuthenticationContext authContext = new AuthenticationContext(authority);
//I think the problem is here:
var token = authContext.AcquireTokenAsync(resource, credential).Result.AccessToken;
return token;
}
public string MakeRequest()
{
string accessToken = GetToken();
var tenantId = "";
string graphResourceId = "https://graph.windows.net/";
Uri servicePointUri = new Uri(graphResourceId);
Uri serviceRoot = new Uri(servicePointUri, tenantId);
ActiveDirectoryClient client = new ActiveDirectoryClient(serviceRoot, async () => await Task.FromResult(accessToken));
foreach (var user in client.Users.ExecuteAsync().Result.CurrentPage)
Console.WriteLine(user.DisplayName);
var client1 = new HttpClient();
var uri = "https://graph.windows.net/" + tenantId + "/users?api-version=1.6";
client1.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
var response = client1.GetAsync(uri).Result;
var result = response.Content.ReadAsStringAsync().Result;
return result;
}
我不知道问题出在哪里,我也没有发现任何好的提示,在其他问题下,稍作解释会有所帮助。当然,我想了解这一部分。IIS进程中没有使用Console.WriteLine的输出。如果要在web项目的输出窗口中输出结果,可以使用System.Diagnostics.Debug.WriteLine方法 //当你打电话的时候 主{Method_A} aync方法{等待方法} 任务
任务方法_B{return T;}您得到的详细错误是什么以及哪行代码?代码似乎是正确的。您是否有足够的权限列出用户?例如,我们需要User.Read.All或Directory.Read.All权限来执行此REST。您可以从中解码访问令牌并检查角色claim.iisexpress.exe信息:…-AcquireTokenHandlerBase:==令牌获取已开始:权限:资源:客户端ID:。。。。CacheType:Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache 0项身份验证目标:客户端Microsoft.IdentityModel.Clients.ActiveDirectory信息:2:…-TokenCache:在缓存iisexpress.exe中未找到匹配的令牌信息:0:…-令牌缓存:在缓存中找不到匹配的令牌。角色似乎还行,这是adal的日志。当您第一次执行该方法时,可能无法从缓存中获取令牌。你的意思是你没有任何异常,只得到上面的日志吗?是的,我没有得到异常。我试着在按下一个按钮后显示结果,我得到了这个,网站刚刚加载。。。也许应该尝试异步编写它?哦,谢谢!但还是不起作用。。。我不知道为什么装载和没有得到令牌。在该行之后获取日志:var token=authContext.AcquireTokenAsyncresource,credential.Result.AccessToken;您开发的ADAL版本是什么?您是如何检查日志的?如果可以检查代码是否按预期工作,我还建议您调试代码。ADAL版本是2.14.0.0,我在前面提到的那行之后得到了此日志。代码似乎是正确的。您能否成功获取访问令牌?既然没有错误和异常,你能解释什么是不工作吗?System.Diagnostics.Debug.WriteLine将输出发送到Visual Studio的输出窗口。您是否通过该窗口或文本文件检查了日志?正如我看到的,问题是权限,但我在Azure Ad中添加了权限,但例外情况是:{odata.error:{code:Authorization\u RequestDenied,message:{lang:en,value:权限不足,无法完成操作。}