我试图从我的网络应用程序中的Microsoft图表中获取数据.
当我调用AcquireTokenSilentAsync()时,我收到错误“无法以静默方式获取令牌.调用方法AcquireToken”.
因此,我尝试使用AcquireTokenAsync()方法.这会获得一个令牌,但是当我尝试访问该资源时,我得到403 – 禁止.
我在Fiddler测试了它,它的工作原理.
当我从AcquireTokenAsync()检查令牌并将其与从fiddler获得的令牌进行比较时,它大约是长度的1/3.我不知道这是否是一个问题,是否有修复?
有人知道这个问题的解决方案吗?
我的代码如下:
为gettoken():
public async static Task<AuthenticationResult> GetTokenAsync(AuthenticationContext ctx, string resourceId)
{
ClientCredential credential = new ClientCredential(OfficeSettings.ClientId, OfficeSettings.ClientSecret);
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
UserIdentifier ident = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);
var redirectUrl = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
try
{
var result = await ctx.AcquireTokenSilentAsync(resourceId, credential, ident);
//var result = await ctx.AcquireTokenAsync(resourceId, credential);
LastAuthority = ctx.Authority;
return result;
}
catch (AdalException e)
{
ctx.TokenCache.Clear();
return null;
}
catch (Exception ex)
{
throw ex;
}
}
GetUserEmail()(访问资源):
private const string _allUsersUrl = "https://graph.microsoft.com/beta/users?$filter=displayName%20eq%20'{0}'";
public static async Task<List<string>> GetUserEmails(List<string> displayNames)
{
var emails = new List<string>();
using (var client = new HttpClient())
{
foreach (var name in displayNames)
{
var url = string.Format(_allUsersUrl, name.Replace(" ", "+")).Replace(" ", "%20");
using (var req = new HttpRequestMessage(HttpMethod.Get, url))
{
var token = await GetToken();
req.Headers.Add("Authorization", string.Format("Bearer {0}", token));
req.Headers.TryAddWithoutValidation("Content-Type", "application/json");
using (var response = await client.SendAsync(req))
{
//TODO: Forbidden error message. However, token seems to be retrieved correctly
var content = await response.Content.ReadAsStringAsync();
foreach (var item in JObject.Parse(content)["value"])
{
emails.Add(item["userPrincipalName"].ToString());
}
}
}
}
}
return emails;
}
编辑:
我还检查了缓存的内容,并将其与传递给AcquireTokenSilentAsync()的参数进行了比较,一切似乎都匹配.
最佳答案 我发现的唯一解决方案是创建一个新的Azure应用程序,其权限与另一个完全相同,并将我的应用程序指向新的客户端ID和客户端密钥.我不知道为什么会这样,但确实如此,我现在可以查询图表了.我希望这有助于将来的某些人.