Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

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

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
    XM-22040801-0 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
    Adarsh S 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
    XM-22040801-0 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
    Adarsh S 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
    Guido Preite 54,081 Super User 2024 Season 1 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans