我使用以下代码获取访问令牌。
它正在工作一段时间然后抛出错误,如
您的访问令牌已过期请在提交请求之前续订
public static async Task<string> GetTokenForUser()
{
string strAutherizationString = ConfigurationManager.AppSettings["AuthrizationURL"].ToString();
string strResourceURL = ConfigurationManager.AppSettings["ResourceURL"].ToString();
string strDirectoryId = ConfigurationManager.AppSettings["DirectoryId"].ToString();
string strClientId = ConfigurationManager.AppSettings["ApplicationId"].ToString();
string strSecretKey = ConfigurationManager.AppSettings["SecurityKey"].ToString();
AuthenticationContext authenticationContext = new AuthenticationContext(strAutherizationString + strDirectoryId, false);
try
{
if (TokenForUser == null)
{
ClientCredential clientCred = new ClientCredential(strClientId, strSecretKey);
AuthenticationResult userAuthnResult = await authenticationContext.AcquireTokenAsync(strResourceURL, clientCred);
TokenForUser = userAuthnResult.AccessToken;
//Capturing the token for the user
log.Info("GetTokenForUser()" + TokenForUser);
}
return TokenForUser;
}
catch (Exception ex)
{
//Capturing the error log for the user token
log.Error("GetTokenForUser()", ex);
throw ex;
}
finally
{
authenticationContext.TokenCache.Clear();
}
}
任何人都可以帮助我吗?