web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Is it possible to request a new AccessToken by passing a Refresh Token using AuthenticationContext class in C#?

(0) ShareShare
ReportReport
Posted on by 792

Hello everyone,

I have followed the Client sample code section in this page https://docs.microsoft.com/en-us/dynamics365/unified-operations/dev-itpro/data-entities/services-home-page#authentication to generate AccessToken for use in calling OData services.

Code snippet:-

UriBuilder uri = new UriBuilder ("login.windows.net/contoso2ax.onmicrosoft.com");
string aadClientAppId = "someappid";

AuthenticationContext authenticationContext = new AuthenticationContext(uri.ToString());

var userCredential = new UserPasswordCredential(username, password);
authenticationResult = authenticationContext.AcquireTokenAsync("usnconeboxaos.cloud.dynamics.com", aadClientAppId, userCredential).Result; 

string authenticationHeader = authenticationResult.CreateAuthorizationHeader();

But these AccessTokens are short-lived and expires after 30mins. So I was requesting a new AccessToken everytime it expires (basically calling this same snippet of above code). 

But now I learnt that there is a concept of refresh tokens, passing which you can renew your access token. I have also gone through this blog https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-protocols-oauth-code. But I want to know whether there is any way to renew access token using refresh token  through AuthenticationContext APIs?

And also, which one of these 2 methods better? Renewing AccessToken by calling the above code snippet every time, or using Refresh token?

Thanks.

*This post is locked for comments

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans