Skip to main content

Notifications

Customer Service forum

MS Dynamics 365 : Generate Refresh Token for OAuth

Posted on by Microsoft Employee

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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: MS Dynamics 365 : Generate Refresh Token for OAuth

    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);

  • Suggested answer
    RE: MS Dynamics 365 : Generate Refresh Token for OAuth

    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

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans