Refreshing the Users Login Token

David Downing 701 Reputation points
2022-03-03T18:43:55.323+00:00

We're using msal to acquire a token that is used to call multiple Azure Rest APIs in our JavaScript authentication code (on behalf of the logged on user). When invoking the rest APIs, a series of calls are needed to perform the task and occasionally, in the C# controller processing the requests the token times out.

Is it possible from the C# controller to refresh the user login token by calling into a function in the JavaScript code?

Thank you.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
20,629 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 23,216 Reputation points Microsoft Employee
    2022-03-03T23:07:31.64+00:00

    Hi @David Downing , thanks for the question. Yes, you can force a refresh for the user token. From this thread:

    By default, access tokens expire after 1h, and if AAD is busy when the tokens expire, your application will become unavailable because you cannot acquire a valid access token. You can improve the availability of your application by regularly forcing a refresh. We recommend to force a refresh every 30 min, or half the lifetime of the AT when this is a custom lifetime.

    result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault())  
                 .WithForceRefresh(true)  
                 .ExecuteAsync();  
    

    If this answer helped you please mark it as "Verified" so other users can reference. Please let me know if you have any questions!

    Thank you,
    James

    1 person found this answer helpful.