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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer Service forum

MS Dynamics 365 : Generate Refresh Token for OAuth

(0) ShareShare
ReportReport
Posted on by

Hello,

 

We are using OData api of MS Dynamics. Here we are doing authentication using oauth

 

We are taking reference from

Ref : https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/developer-guide?view=dynamics-ce-odata-9

 

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/connect-customer-engagement-web-services-using-oauth

 

we are using Web Api so we followed below reference.

 

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/oauth-cross-origin-resource-sharing-connect-single-page-application

 

Steps we have follow :

  1. Registered an app to Microsoft Azure:

Ref: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/walkthrough-register-dynamics-365-app-azure-active-directory

2. Acquire token using ADAL.js.

-          Tried with ADAL C# sdk but it gets failed as dialog for login window could not pop out into Asp.Net Core Web Api project.

Ref : https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/connect-customer-engagement-web-services-using-oauth

 

To Generate token we are only using ApplicationId , ResourceUrl (dynamics resource url).

3. Pass as Bearer token to the rest api’s . It worked.

Need : We have to refresh token, if the token get expired. We tried using c# ADAL SDK that is specified into the document itself.

 AuthenticationContext authContext =

                    new AuthenticationContext("https://login.windows.net/common/", false);

       AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientId, new Uri(redirectUrl), new PlatformParameters()); 

               

Exception : The method or operation is not implemented.

at Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Platform.WebUIFactory.CreateAuthenticationDialog(IPlatformParameters parameters)

 

Can you guide us, how can we refresh token in Web Api (Asp.net Core). And how can we get refresh_token in MS Dynamics OAuth. Do you have any other approach to access token / refresh token?

 

Note : We only allow login oauth dialog box from html page once and store the given token. We have to use either same token to generate new token or any other way to refresh token(without prompt dialog second time) to be use in MS Dynamics OData Web Api.

We are using Asp.net Core Web API Project to perform interaction between dynamics.

I have the same question (0)
  • Suggested answer
    Anupam.Khasia Profile Picture
    183 Super User 2026 Season 1 on at

    Hello Vijay,

    You can use below method to generate first time token & than refresh token without login again.

    result = authContext.AcquireTokenSilentAsync(todoListResourceId, clientId).Result;

    You can find sample code for same from below link :

    docs.microsoft.com/.../active-directory-v2-libraries

  • Community Member Profile Picture
    on at

    Here is what you can do:

    // Everytime you send a request to CRM Web Api, do:

    // This basically acquires a token if it is stored in a cache, if it expires it will them try to retrieve another one and start all over again.

    AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientId, new Uri(redirectUrl), new PlatformParameters());

    // It is a best practice to refresh the access token before every message request is sent. Doing so

    // avoids having to check the expiration date/time of the token. This operation is quick.

    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Customer Service

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans