TokenCache:在缓存中找不到匹配的令牌,Azure AD Api
TokenCache: No matching token was found in the cache, Azure AD Api
我想使用 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/{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项目如果想输出window的结果,可以使用System.Diagnostics.Debug.WriteLine()
方法。
- //调用时
主要() { Method_A() }
aync Method_A() { await Method_B() }
任务 Method_B() { return T; }
//会通过报错。 //需要在另一个Task中保留Mehtod_B和运行.
// 这里我避免了一些异步
主要() { Method_A() }
Method_A() { Method_B().Wait() }
任务 Method_B() { return T; }
我想使用 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/{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项目如果想输出window的结果,可以使用System.Diagnostics.Debug.WriteLine()
方法。
- //调用时
主要() { Method_A() }
aync Method_A() { await Method_B() }
任务
//会通过报错。 //需要在另一个Task中保留Mehtod_B和运行.
// 这里我避免了一些异步
主要() { Method_A() }
Method_A() { Method_B().Wait() }
任务 Method_B() { return T; }