How to use S2S Authentication to connect with Dynamics CE using Azure Functions?

1_IegFtGg3n6FiCeik2PDuhAOne of my customers requested to implement APIs which should execute some connected business logic using Dynamics CE and other Azure and non-Microsoft services. The APIs had to be publically available to access from other cloud services my customer had. To implement these requirements, we decided to use Azure Functions together with S2S Authentication. In this article, I would like to give you some examples of how to connect to Dynamics CE from Azure Functions using S2S authentication.

First of all, you need to understand and configure S2S authentication. Please check the following article to get more details: Use Single-Tenant server-to-server authentication. Once you’ve set S2S authentication for your Dynamics CE environment, you can start creating Azure Functions. Let’s first create a service function which should request Bearer Token for us. I will keep it simple without cashing or any additional logic. In a real-world scenario, you can implement caching and automatic token refresh to avoid extra service calls. By default, the token will be valid for one hour. Some more information you can find here: Configurable token lifetimes in Azure Active Directory.

In the example above, I will just hardcode parameters which we need to connect to Dynamics 365. You should take these parameters from your Azure Portal during the S2S authentication process configuration. To store these settings securely, you can use Azure Key-Vault. Here you find more information about using Azure Key-Vault: Tutorial: Use Azure Key Vault with an Azure web app in .NET

The next Azure function should call Dynamics CE and extract some data from it. Here is the example which shows how to execute one azure function from another to get authentication token and extract some sample data from the Dynamics CE environment.

The second function uses our first function to get Bearer token and then call Web API to get data from contacts entity.

Don’t forget to add Microsoft.IdentityModel.Clients.ActiveDirectory dependency to your Azure functions project.

You can create several functions which will call different operations in CRM and return results to anonymous users. You can also configure security checks for your azure functions. More information and some examples about it you can find here: Azure Functions HTTP triggers and bindings.

I hope you got the idea of how to use S2S authentication in Azure functions and connect/extract data from your Dynamics CE organization.

Leave a Reply