Skip to content
Go back

D365 - Connecting Console App with CRM - Office 365 AuthType

Table of contents

Open Table of contents

Introduction

This article demonstrates how to connect to Dynamics 365 (D365) using Office365 authentication within a Console Application (.NET Framework).

In fact, Microsoft.Xrm.Tooling.Connector provides multiple authentication types, but based on my project experience, the two most frequently used are: Office365 and ClientSecret.

List of Authentication Types:

#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

1. Create a Console Application for Testing

Step 1. Create a New Project

  1. Open Visual Studio and create a new Console App (.NET Framework) project.
  2. Enter a meaningful project name → select the framework → click Create.

New Project - 01

New Project - 02


Step 2. Add Dependencies to the Project

Required packages:

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

Right-click your project → Manage NuGet Packages.

Add Dependencies - 01

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

From the results, select Microsoft.CrmSdk.CoreAssemblies → click Install → click Accept in the popup.

Add Dependencies - 02

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


Step 3. Add Connection Information to App.config

In App.config, add your connection information (connectionStrings), replacing the placeholders with your environment details:

Add Connection Info in App.config

<connectionStrings>
  <!-- Dev Environment (Office365 Authentication) -->
  <add name="Dev-Office365" 
       connectionString="AuthType=Office365; 
       Url=urlofyourdynamics365instance; 
       Username=yourusername; 
       Password=yourpassword;" />
</connectionStrings>

Step 4. Add Test Code

Add Test Code

  1. Open Program.cs.
  2. Replace the using statements with the following:
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;
  1. Add the following code inside the Main method:
string connectionStr = ConfigurationManager.ConnectionStrings["Dev-Office365"].ConnectionString;
CrmServiceClient client = new CrmServiceClient(connectionStr);
if (client.IsReady)
{
    IOrganizationService orgService = client;
    // Test with 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. Run the program using F5 or the Start button at the top. Run Test Project

If everything works correctly, the user’s Id will be printed. From here, you can proceed to perform CRUD operations with IOrganizationService.

Print User Id

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 - Currency Fields
Next Post
D365 - Commonly Used SQL