【发布时间】:2016-11-29 15:05:09
【问题描述】:
我正在尝试使用我的应用程序访问图形 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 的调用以通过调用获取刷新令牌?
【问题讨论】:
标签: c# asp.net azure-ad-graph-api