Microsoft has started depreciating the Office365 Authentication Type as announced earlier. Microsoft recommends to use OAuth authentication type for CRM connectivity through code. Let’s see how can we do it:
Register an app in Azure Active Directory
Login to Azure Portal and search for Azure Active Directory.
![Connect-to-CRM-using-OAuth-in-C-1](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-1-1024x656.png)
Click on App Registrations.
![Connect-to-CRM-using-OAuth-in-C-Sharp-2](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-2-1024x645.png)
Click New Registration and Register.
![Connect-to-CRM-using-OAuth-in-C-Sharp-3](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-3-1024x614.png)
![Connect-to-CRM-using-OAuth-in-C-Sharp-4](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-4-1024x620.png)
Once Registered, click on Manifest and set the allowPublicClient property to true.
![Connect-to-CRM-using-OAuth-in-C-Sharp-5](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-5-1024x616.png)
To set Permissions, Click on API Permissions.
![Connect-to-CRM-using-OAuth-in-C-Sharp-6](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-6-1024x616.png)
Click on Add Permission to add permissions for Dynamics CRM.
![Connect-to-CRM-using-OAuth-in-C-Sharp-7](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-7-1024x553.png)
Select Access Common Data Service as organization users.
![Connect-to-CRM-using-OAuth-in-C-Sharp-8](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-8.png)
Once permissions are updated, we will see the following.
![Connect-to-CRM-using-OAuth-in-C-Sharp-9](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-9-1024x661.png)
Next, we will copy the Application (client) ID by clicking on Overview.
![Connect-to-CRM-using-OAuth-in-C-Sharp-10](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-10-1024x665.png)
Click on Certificates & Secrets and generate the new secret by clicking on New Client Secret.
![Connect-to-CRM-using-OAuth-in-C-Sharp-11](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-11-1024x660.png)
Copy the newly generated secret for use in our code.
![Connect-to-CRM-using-OAuth-in-C-Sharp-12](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-12-1024x572.png)
Next step is to create an Application User in CRM.
![Connect-to-CRM-using-OAuth-in-C-Sharp-13](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-13-1024x651.png)
While creating the Application User, please add the Application ID copied earlier.
![Connect-to-CRM-using-OAuth-in-C-Sharp-14](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-14-1024x651.png)
Once the user is added, please assign a security role to the user.
Connecting to CRM through code
We will now create a console application in visual studio. Please make sure that .NET Framework is 4.6.2 or later.
Please add the Microsoft.CrmSdk.XrmTooling.CoreAssembly package in your project from NuGet Packages.
![Connect-to-CRM-using-OAuth-in-C-Sharp-15](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-15-1024x655.png)
In order to use OAuth, the connection string should be in a format as defined here.
![Connect-to-CRM-using-OAuth-in-C-Sharp-16](https://optimallogics.com/wp-content/uploads/2020/11/Connect-to-CRM-using-OAuth-in-C-Sharp-16.png)
In order to verify that we are able to connect successfully to CRM, we will use the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using (CrmServiceClient svcClient = new CrmServiceClient(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { if (!svcClient.IsReady) { Console.WriteLine("unable to connect!"); return; } WhoAmIRequest request = new WhoAmIRequest(); WhoAmIResponse response = (WhoAmIResponse)svcClient.Execute(request); Console.WriteLine("UserId =" + response.UserId.ToString()); } |
Now, you are ready to connect your code to CRM using OAuth. We hope you find this article interesting and helpful.
For questions or insights, leave a comment below or reach out to us at info@optimallogics.com. We are always happy to help.