web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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.

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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';

  • Adarsh S Profile Picture
    5 on at

    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

  • XM-22040801-0 Profile Picture
    11 on at

    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

    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

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans