Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get refresh token with Azure AD V2.0 (MSAL) and Asp .Net Core 2.0

I've got access_token from Azure Ad V2.0 endpoint to call Graph Api. But I have to do some actions in the api on behalf of user. So I need refresh_token to renew my access_token when it'll expire.

Is there any way to get Refresh token using MSAL in ASP .Net Core?

In microsoft documentaion they're telling it's possible to do by requesting /token endpoint. But I couldn't find how to do it using MSAL.

like image 721
S. Anna Avatar asked Feb 23 '18 16:02

S. Anna


People also ask

How do I get Msal access token?

Get an access token to call an API In MSAL, you can get access tokens for the APIs your app needs to call using the acquireTokenSilent method which makes a silent request(without prompting the user with UI) to Azure AD to obtain an access token.

How do I get a new refresh token OAuth2?

Because OAuth2 access expires after a limited time, an OAuth2 refresh token is used to automatically renew OAuth2 access. Click the tab for the programming language you're using, and follow the instructions to generate an OAuth2 refresh token and set up the configuration file for your client.


Video Answer


2 Answers

I got a bit topsy-turvy on this, as well. Explaining a bit more based on my understanding.

  • For context, OAuth 2.0 code grant flow mentions the following steps:
    • authorization, which returns auth_code
    • using auth_code, to fetch access_token (usually valid for 1 hr) and refresh_token
    • access_token is used to gain access to relevant resources
    • after access_token expires, refresh_token is used to get new access_token
  • MSAL.NET abstracts this concept of refresh_token via TokenCache.
    • There is an option to serialize TokenCache. See Token cache serialization in MSAL.NET. This is how to preserve sign-in info b/w desktop application sessions, and avoid those sign-in windows.
    • AcquireTokenSilentAsync is the process by which refresh_token is used to get new access_token, but, this is internally done. See AcquireTokenSilentAsync using a cached token for more details and other access patterns.

Hope this clarifies on why TokenCache is the 'new' refresh_token in MSAL.NET, and TokenCache is what you would need to serialize and save. There are libraries like Microsoft.Identity.Client.Extensions.Msal that aid in this.

like image 62
AAATechGuy Avatar answered Sep 22 '22 13:09

AAATechGuy


MSAL .NET does not expose the refresh token, but rather keeps it internal and handles all token refresh and caching logic on the app's behalf.

The docs you're referring to are referencing the protocol itself that MSAL is completing on your behalf. It goes to the /token endpoint with an authorization code (after the end user signs in), and is issued an Access and Refresh token. The Access Token is valid for 1 hour, and when it's expired, AcquireTokenSilent will automatically use the refresh token against the /token endpoint to get a new access token.

like image 29
Daniel Dobalian Avatar answered Sep 23 '22 13:09

Daniel Dobalian