Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I get refresh token

i learn this code sample :https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web ,and yes ,i can get access token in AuthorizationCodeReceived : AuthenticationHelper.token = result.AccessToken;

but how do i get the refresh token ?result.RefreshToken is not available , then how do i use acquiretokenbyrefreshtoken function ?

https://msdn.microsoft.com/en-us/library/microsoft.identitymodel.clients.activedirectory.authenticationcontext.acquiretokenbyrefreshtoken.aspx

like image 950
Phoenix Avatar asked Mar 21 '17 07:03

Phoenix


People also ask

Where is refresh token stored?

If your application uses refresh token rotation, it can now store it in local storage or browser memory. You can use a service like Auth0 that supports token rotation.

How do you refresh token after it expires?

The member must reauthorize your application when refresh tokens expire. When you use a refresh token to generate a new access token, the lifespan or Time To Live (TTL) of the refresh token remains the same as specified in the initial OAuth flow (365 days), and the new access token has a new TTL of 60 days.

Can we get refresh token from access token?

To get an access token using a refresh token, you must first get the refresh token. Then you use the refresh token from then on to generate an access token.

What if refresh token is stolen?

If the refresh token becomes compromised, it is less likely to be valid, preventing an unauthorized user from gaining access to secure resources.


2 Answers

The acquiretokenbyrefreshtoken function is available in ADAL 2.X , that code sample is using ADAL 3.13.8 , and from ADAL3.X, library won't expose refresh token and AuthenticationContext.AcquireTokenByRefreshToken function.

ADAL caches refresh token and will automatically use it whenever you call AcquireToken and the requested token need renewing(even you want to get new access token for different resource).

please see the explanation from here . Also click here and here for more details about refresh token in ADAL .

like image 177
Nan Yu Avatar answered Oct 21 '22 11:10

Nan Yu


If you looking for a persistent mechanism, you can simply use TokenCache.Serialize()

Here's how I did it:

First, get the token and serialize the cache token

AuthenticationContext authContext = new AuthenticationContext($"https://login.microsoftonline.com/{Tenant}");
var authResult = authContext.AcquireTokenAsync(resource, ClientId, new Uri("https://login.microsoftonline.com/common/oauth2/nativeclient"), new PlatformParameters(PromptBehavior.SelectAccount)).Result;
byte[] blobAuth = authContext.TokenCache.Serialize();

Then, load the cached bytes

AuthenticationContext authContext = new AuthenticationContext($"https://login.microsoftonline.com/{tenant}/");
authContext.TokenCache.Deserialize(blobAuth);
var res = authContext.AcquireTokenSilentAsync(resource, clientId).Result;
like image 24
Yinon_90 Avatar answered Oct 21 '22 11:10

Yinon_90