Skip to content
Go back

D365 - Connecting Console App with CRM - ClientSecret AuthType

Table of contents

Open Table of contents

Introduction

This article discusses how to connect to D365 using the ClientSecret authorization method in a console application (.NET Framework).

The Microsoft.Xrm.Tooling.Connector provides various authorization methods, but based on my project experience, the two most commonly used are Office365 and ClientSecret.

List of Authorization Methods:

#NameValueDescription
1InvalidConnection-1Invalid connection
2AD0Active Directory Auth
3Live1Live Auth
4IFD2SPLA Auth
5Claims3CLAIMS based Auth
6Office3654Office365 base login process
7OAuth5OAuth based Auth
8Certificate6Certificate based Auth
9ClientSecret7Client Id + Secret Auth type
10ExternalTokenManagement99Host manages Auth token for CRM connections

Detailed Steps

Prerequisites: You must be an “Administrator” user to access the Azure Active Directory portal.

1. Obtain ClientId and ClientSecret

Step 1: Register an Application

(1) Visit and log in to the Azure Portal.

(2) In the top search box, search for “App registrations” and click on it.

App registrations-01

(3) Click “New registration.”

App registrations-02

(4) In the pop-up window, enter a meaningful application name (e.g., D365-GetClientSecret) —> Check the supported account types —> Click the “Register” button.

App registrations-03

Step 2: Get ClientId

ClientSecret is a password-like key that effectively prevents unauthorized access. Compared to using a username and password, ClientSecret provides a more secure authentication method and reduces the risk of data breaches.

After completing Step 1, you can find the Application ID (ClientId) in the “Overview” section. Copy it to a text file, as you will need it later.

Get ClientId

Step 3: Get ClientSecret

Select “Certificates & secrets” in the menu bar —> Client secrets —> “+ New client secret” —> Fill in a meaningful description in the pop-up window —> Check the expiration date —> Click the “Add” button.

Get ClientSecret - 01

After adding the “Client secret,” it will appear in the list. Copy it to a text file, as you will need it later (the “Value (ClientSecret)” will be encrypted after the page refreshes; if you do not copy it in time, you can delete it and recreate it).

Get ClientSecret - 02

2. Add Application User

(1) Log in to the “Power Platform Admin Center” —> Select the environment —> Click “Settings.”

(2) Select “Application users” in the menu.

Select application users

In the top menu bar, choose “+ Add a new application user” —> Select “App” (the app you created in Step 1) —> Select the business unit (it is recommended to select “Root business unit”) —> Choose the security role “System Administrator” —> Click the “Create” button.

3. Create Console Application for Testing

Step 1: Create a New Project

  1. Open Visual Studio and create a new Console Application (.NET Framework) project.

  2. Enter a meaningful project name —> Select the framework —> Click “Create.”

Step 2: Add Dependencies to the Project

  1. Microsoft.CrmSdk.CoreAssemblies
  2. System.Configuration.ConfigurationManager

Right-click your project —> Manage NuGet Packages.

In the new window, select the “Browse” tab and search for Microsoft.CrmSdk.CoreAssemblies.

Select Microsoft.CrmSdk.CoreAssemblies from the results and click the Install button on the right. Click “Accept” in the pop-up window.

Now the Microsoft.CrmSdk.CoreAssemblies dependency is installed. Please repeat the same steps to install the System.Configuration.ConfigurationManager dependency.

Step 3: Add Connection Information to App.config

In App.config, add connection information (connectionStrings) and replace the following information with your details:

Add Connection Information to App.config

<connectionStrings>
  <!-- Dev environment (ClientSecret authentication method) -->
  <add name="Dev-ClientSecret" connectionString="
     Url=urlofyourdynamics365instance;
     AuthType=ClientSecret;
     ClientId=yourClientId;
     ClientSecret=yourClientSecret;" />
</connectionStrings>

Step 4: Add Test Code

Add Test Code

(1) Open Program.cs.

(2) Replace the using directives at the top with the following:

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;

(3) In the Main method, add the following code:

string connectionStr = ConfigurationManager.ConnectionStrings["Dev-ClientSecret"].ConnectionString;
CrmServiceClient client = new CrmServiceClient(connectionStr);
if (client.IsReady)
{
    IOrganizationService orgService = client;
    // Test using WhoAmI
    WhoAmIResponse resTest = (WhoAmIResponse)orgService.Execute(new WhoAmIRequest());
    Console.Write($"UserId: {resTest.UserId}");
    Console.Read();
}
else
{
    throw new Exception(client.LastCrmError);
}

Step 5: Test

(1) Right-click your project —> Set as Startup Project.

Set as Startup Project

(2) Use F5 or click the Start button at the top to run the program.

run the program

If everything is working correctly, it will print the UserId, and you can then try using IOrganizationService for CRUD operations.

Print the UserId

The End

Copyright Notice

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.


Share this post on:

Previous Post
D365 - Canvas App Sharing with Security Groups
Next Post
D365 - Set Editable Subgrid Columns to Read-Only Using