Access Dynamics CRM entities in Flutter using AAD

In this blog, we will be discussing how you can secure flutter apps using Azure Active Directory Authentication, through the comfort of your using your organization’s Azure account. You can use the token received from the authentication to access the Dynamics CRM entities.    

How to setup Azure Active Directory App?  

For this blog, we will be setting up an Azure authentication that allows users to log in to the app using their organization accounts and access Dynamics CRM entities. Azure Active Directory authentication helps us add a security layer to the apps and get their information associated with their Azure Active Directory account, such as name, image, etc. Let’s begin!  

To log in, users are navigated to a web view where users need to fill their Active Directory account credentials. After a successful login, the control is transferred back to the flutter app, and the user navigates to the app.   

In the first step, we will set up an Authentication App in Azure Active Directory. To set up, we will need to perform the following steps:    

  • Open Microsoft Azure in your browser     
  • Log in with your Azure account.     
  • From the drawer menu, navigate to “Azure Active Directory.”     
  • In “Azure Active Directory,” browse to “App registrations.”     
  • Click on new registration and register a new app.     
  • From your newly registered app copy, client ID, and tenant ID, use it in the flutter app.    

Integrate Flutter App with Azure Active Directory App 

Now let’s move to step two. Initialize a flutter app; if you are new to flutter and need help setting up a flutter project, you can seek guidance from Write your first app. After setting up the flutter project, we need to add the “Azure Active Directory OAuth” package to it. If you do not already have the latest package installed, you can download it the link below: 

aad_oauth 1.0.1 

After getting the latest package version, add it to your project pubspec-yaml file under dependencies. 

Dependencies: 

aad_oauth: ^0.1.9 // at the moment latest package version is 0.1.9 

After adding a dependency, import the AAD OAuth to your dart file.  

import ‘package:aad_oauth/aad_oauth.dart’; 

Now we need to add our Azure Active Directory app credentials to the flutter AAD configurations. We will be using the tenant and client id we copied from the azure active directory app for this.  

 

Please Note: If you plan to access Dynamics CRM entities in your app, you will need to add your Dynamics CRM link so the token can be used to access entities.    

add App

After the configuration is complete, use the following function to call the webview to log in. 

webview

The token you will receive in return for your Azure authentication can be used to access CRM entities. To access CRM entities, we will be using the Dynamics CRM endpoints and the http package in the flutter. To get the latest version of the package, click on HTTP

After adding the dependency, import the http package to your dart file.  

import ‘package:http/http.dart’ as http; 

 

Access Dynamics CRM entities 

Now you can access Dynamics CRM entities using the token received after the active directory authentication.  

 String url = entity name; 

http.Response response = await http.get(url, headers: { 

        “Accept”: “application/json”, 

        “Content-Type”: “application/json”, 

        “OData-MaxVersion”: “4.0”, 

        “OData-Version”: “4.0”, 

        “Authorization”: “Bearer $accessToken” }); 

 

After following the steps as mentioned above, you have successfully linked your app authentication with your organization Azure Active Directory and can now access your Azure CRM entities. 

If you have any questions or insights on the topic, please do share them using the comment box below!

Happy accessing!