简体   繁体   English

AcquireToken async在到期时间之前返回相同的令牌

[英]AcquireToken async returning same token before expiry time

I have registered my application with Azure AD App registration. 我已使用Azure AD App注册注册了我的应用程序。

In my scenario i am using Azure Adal AquireTokenAsync method with client credentials which is always returning same token. 在我的场景中,我使用Azure Adal AquireTokenAsync方法和客户端凭据,该方法始终返回相同的令牌。

i need a new token for every user session. 我需要为每个用户会话添加一个新令牌。

for testing purpose i have created console application to test the behaviour. 出于测试目的,我已经创建了控制台应用程序来测试行为。

string authority = String.Format(CultureInfo.InvariantCulture, 
      ConfigurationManager.AppSettings["ida:AADInstance"], 
      ConfigurationManager.AppSettings["ida:Tenant"]);

        AuthenticationContext authContext = new AuthenticationContext(authority, false);
        ClientCredential clientCredential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientId"], ConfigurationManager.AppSettings["ida:AppKey"]);
        AuthenticationResult result = null;
        int retryCount = 0;
        bool retry = false;
        retry = false;
        try
        {
            result = await authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["ida:ResourceId"], clientCredential);
            result = await authContext.AcquireTokenAsync(ConfigurationManager.AppSettings["ida:ResourceId"], clientCredential);
        }
        catch (AdalException ex)
        {

        }
        finally
        {
            authContext = null;
        }

In both the calls it is returning the same token. 在两个调用中,它返回相同的标记。

however for every fresh execution it is returning new token. 但是对于每次新执行,它都会返回新的令牌。

It's because inside AuthenticationContext , there is TokenCache to cache the id_token . 这是因为在AuthenticationContext ,有TokenCache来缓存id_token So, if you would like to have new token every time calling AcquireTokenAsync , set TokenCache is null when creating AuthenticationContext object: 所以,如果你想每次有新的令牌调用AcquireTokenAsync ,设置TokenCache创建时为空AuthenticationContext对象:

AuthenticationContext authContext = new AuthenticationContext(authority, false, null);

Please prefer the link 请更喜欢链接

ADAL context cache keep token.If you need to get it refreshed please clear the cache using ADAL上下文缓存保留令牌。如果您需要刷新它,请使用清除缓存

  authContext.TokenCache.Clear();

it will clear the cache. 它将清除缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM