Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Unable to establish connection between .NET Console app and Dynamics CRM using Azure Active Directory

(0) ShareShare
ReportReport
Posted on by 5

Hello Experts,

I am trying to create a console application in .NET which communicates with CRM so would perform CRUD operation but I am not able to connect getting error always. I am sharing the code snippet.

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

namespace ConsoleAppWithD365
{
    class Program
    {
        static void Main(string[] args)
        {
            CrmServiceClient crmSvc = getOrganizationService();
            Console.ReadLine();
        }

        public static CrmServiceClient getOrganizationService()
        {
            CrmServiceClient crmSvc = null;
            String connectionString = "AuthType=OAuth;Username=xxxx@xxxx.onmicrosoft.com; Password=xxxx;Url=https://xxx.crm.dynamics.com;AppId=xxxx; RedirectUri=http://localhost;LoginPrompt=Auto";
            try
            {;
                crmSvc = new CrmServiceClient(connectionString);
                if (crmSvc != null)
                {
                    var whoAmIResponse = ((WhoAmIResponse)crmSvc.Execute(new WhoAmIRequest()));
                    if (whoAmIResponse != null)
                    {
                        Console.WriteLine("Connection OK....");
                        Console.WriteLine(whoAmIResponse.UserId);
                    }
                    else
                    {
                        Console.WriteLine("Not Connected....");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR...");
                Console.WriteLine(e.Message);
            }
            return crmSvc;
        }
    }
}



I have added Dynamics 365 SDK references to the project via NuGet.

While using above code getting below error.

pastedimage1683725146387v1.png

I have registered the Application to the azure, Generated the Client secret also created the application user with the current application id by following steps given here : https://blog.magnetismsolutions.com/blog/paulnieuwelaar/2021/9/21/setting-up-an-application-user-in-dynamics-365

Still getting error.

I can not use the Auth type Office 365 as it is depreciated. Please help to generate the OrgService to perform the CRUD operation in dynamics 365 using console app.

  • XM-22040801-0 Profile Picture
    11 on at
    RE: Unable to establish connection between .NET Console app and Dynamics CRM using Azure Active Directory

    Your screenshots do not share the real error.

    Can you please share the LastCrmError property of CrmServiceClient? See learn.microsoft.com/.../microsoft.xrm.tooling.connector.crmserviceclient.lastcrmerror

  • Adarsh S Profile Picture
    5 on at
    RE: Unable to establish connection between .NET Console app and Dynamics CRM using Azure Active Directory

    Hi Xavier Monin 

    I got connection string via XRMToolBox and used in below source code.

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Tooling.Connector;
    using System;
    
    namespace ConsoleAppWithD365
    {
        class Program
        {
            static void Main(string[] args)
            {
                CrmServiceClient crmSvc = getOrganizationService();
                Console.ReadLine();
            }
    
            public static CrmServiceClient getOrganizationService()
            {
                CrmServiceClient crmSvc = null;
                String connectionString = "AuthType=OAuth;Username=xxxx@xxxx.onmicrosoft.com; Password=xxxx;Url=https://xxx.crm.dynamics.com;AppId=xxxx; RedirectUri=http://localhost;LoginPrompt=Auto";
                try
                {;
                    crmSvc = new CrmServiceClient(connectionString);
                    if (crmSvc != null)
                    {
                        var whoAmIResponse = ((WhoAmIResponse)crmSvc.Execute(new WhoAmIRequest()));
                        if (whoAmIResponse != null)
                        {
                            Console.WriteLine("Connection OK....");
                            Console.WriteLine(whoAmIResponse.UserId);
                        }
                        else
                        {
                            Console.WriteLine("Not Connected....");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR...");
                    Console.WriteLine(e.Message);
                }
                return crmSvc;
            }
        }
    }

    Still error is reproducible.

    image

  • XM-22040801-0 Profile Picture
    11 on at
    RE: Unable to establish connection between .NET Console app and Dynamics CRM using Azure Active Directory

    Hi,

    Can you try to connect with the clientid and clientsecret with XrmToolBox.

    pastedimage1683731592692v1.png

    Once done, you can get the connection string via a button.

    pastedimage1683731524785v1.png

  • Adarsh S Profile Picture
    5 on at
    RE: Unable to establish connection between .NET Console app and Dynamics CRM using Azure Active Directory

    Guido Preite  Still not able to establish connection with this connection string AuthType='ClientSecret'; Url='https://abc123.crm.dynamics.com/'; ClientId='xxx'; ClientSecret='yyy'; and getting same error.

    pastedimage1683730770266v2.png

    pastedimage1683730433758v1.png

  • Suggested answer
    Guido Preite Profile Picture
    54,077 Moderator on at
    RE: Unable to establish connection between .NET Console app and Dynamics CRM using Azure Active Directory

    with an application user the connection string is authtype ClientSecret like this one

    AuthType='ClientSecret'; Url='https://abc123.crm.dynamics.com/'; ClientId='xxx'; ClientSecret='yyy';

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans