1

我正在使用下面的代码来获取访问令牌。

它工作了一段时间,然后抛出错误

您的访问令牌已过期,请在提交请求前更新它

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();
    }
}

谁能帮我吗?

4

0 回答 0