I have a managed Identity for the Function App with System Admin role assinged in Dynamics CRM Environment. Shall i perform CRUD operations using the managed identity instead of using Service Principal Credentials in the Code?
Loading
I have a managed Identity for the Function App with System Admin role assinged in Dynamics CRM Environment. Shall i perform CRUD operations using the managed identity instead of using Service Principal Credentials in the Code?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mitchell Clark11 reputationPosted Jun 12, 2024, 4:01 AM EDT
Yes, you can perform CRUD (Create, Read, Update, Delete) operations using the managed identity for your Function App instead of using Service Principal credentials in the code. Managed identities are designed to simplify and enhance the security of accessing Azure resources, including Dynamics CRM environments.
Here's why and how you can use the managed identity for CRUD operations:
Benefits of Using Managed Identity:
Steps to Use Managed Identity for CRUD Operations:
Enable Managed Identity for the Function App: Ensure that the managed identity is enabled for your Function App.
Assign Permissions in Dynamics CRM: Since you've mentioned that the managed identity has the System Admin role assigned in Dynamics CRM, it should already have the necessary permissions for CRUD operations.
Obtain an Access Token: Your Function App will need to obtain an access token to authenticate to Dynamics CRM. Here's a sample code snippet to get an access token using the managed identity:
using Microsoft.Azure.Services.AppAuthentication; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; public async Task GetAccessTokenAsync(string resource) { var azureServiceTokenProvider = new AzureServiceTokenProvider(); string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(resource); return accessToken; }Use the Access Token for CRUD Operations: Once you have the access token, you can use it to authenticate your HTTP requests to the Dynamics CRM API. Here is an example of how you can use the access token to perform a GET request:
using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; public async Task GetEntityDataAsync(string accessToken, string crmUrl) {using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); HttpResponseMessage response = await httpClient.GetAsync(crmUrl); return response; } }Complete Example for CRUD Operations
Here is a more detailed example showing how to perform CRUD operations using managed identity in an Azure Function App:
Enable Managed Identity in Azure Function App: Ensure the managed identity is enabled in the Azure portal for your Function App.
Assign Permissions in Dynamics CRM: Assign the necessary roles and permissions to the managed identity in your Dynamics CRM environment.
Azure Function to Perform CRUD Operations:
using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using Microsoft.Azure.Services.AppAuthentication; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.Extensions.Logging; public static class DynamicsCrmFunction { private static readonly string DynamicsCrmUrl = "https://your-crm-instance.crm.dynamics.com/api/data/v9.1/"; [FunctionName("GetCrmData")] public static async Task GetCrmData( [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequestMessage req, ILogger log) { try { // Obtain the access token using managed identity string accessToken = await GetAccessTokenAsync("https://your-crm-instance.crm.dynamics.com"); // Perform a GET request to Dynamics CRM string requestUrl = DynamicsCrmUrl + "contacts"; HttpResponseMessage response = await GetEntityDataAsync(accessToken, requestUrl); if (response.IsSuccessStatusCode) { string data = await response.Content.ReadAsStringAsync(); return new OkObjectResult(data); } else { return new BadRequestObjectResult($"Failed to get data: {response.ReasonPhrase}"); } } catch (Exception ex) { log.LogError($"Error: {ex.Message}"); return new StatusCodeResult(500); } } private static async Task GetAccessTokenAsync(string resource) { var azureServiceTokenProvider = new AzureServiceTokenProvider(); return await azureServiceTokenProvider.GetAccessTokenAsync(resource); } private static async Task GetEntityDataAsync(string accessToken, string crmUrl) { using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); return await httpClient.GetAsync(crmUrl); } } }Conclusion
By following these steps, you can securely perform CRUD operations on Dynamics CRM using the managed identity assigned to your Azure Function App. This approach enhances security by eliminating the need for hard-coded credentials and leverages Azure's managed identity capabilities for seamless authentication.