我正在尝试使用我的应用程序访问图形api并且它成功,但是,当令牌过期时我没有刷新令牌来刷新我的设置然后我的应用程序停止工作,直到我重新发布应用程序和它会生成一个新令牌。
以下是我通过AJAX调用以从图api中获取数据的代码:
[Authorize]
public async Task<UserProfile> UserProfile()
{
string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;
// Get a token for calling the Windows Azure Active Directory Graph
AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, LoginUrl, tenantId));
ClientCredential credential = new ClientCredential(AppPrincipalId, AppKey);
AuthenticationResult assertionCredential = authContext.AcquireToken(GraphUrl, credential);
string authHeader = assertionCredential.CreateAuthorizationHeader();
string requestUrl = String.Format(
CultureInfo.InvariantCulture,
GraphUserUrl,
HttpUtility.UrlEncode(tenantId),
HttpUtility.UrlEncode(User.Identity.Name));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
HttpResponseMessage response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
UserProfile profile = JsonConvert.DeserializeObject<UserProfile>(responseString);
profile.Username = User.Identity.Name.Replace("@xxx.com", "");
return profile;
}
运行这个,当我得到assertionCredential
时,它有一个访问令牌,accessTokenType和一个到期日期(距创建时间1小时),但刷新令牌为空。我是否需要编辑对API的调用以通过调用获取刷新令牌?