
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)