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

Console app connecting with D365 online

(0) ShareShare
ReportReport
Posted on by 2,510

Hi, I am looking to connect with D365 online through .net console app with the below code, and I got the below error, it looks like I have to create application user and get the application id and secret ? can I connect with my logins ? Please advise.

Error message: "An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail."

 IOrganizationService organizationService = null;
            try
            {
                ClientCredentials clientCredentials = new ClientCredentials();
                clientCredentials.UserName.UserName = "username";
                clientCredentials.UserName.Password = "password";
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://recotest2.api.crm3.dynamics.com/XRMServices/2011/Organization.svc"),
                 null, clientCredentials, null);
                if (organizationService != null)
                {
                    Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
                    if (userid != Guid.Empty)
                    {
                        Console.WriteLine("Connection Successful!...");
                    }
                }
                else
                {
                    Console.WriteLine("Failed to Established Connection!!!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught - "   ex.Message);
            }
            Console.ReadKey();

I have the same question (0)
  • Suggested answer
    Kenneth Ariel Chaves Herrera Profile Picture
    on at

    Hi,

    There is a better practice that is to create the connection in the app.config file, here the code:

    
         
            
        
    	
    		
    	
    

    Then in your program code you just must call the CRrmServiceClient object as following:

    CrmServiceClient oMSCRMConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["MyCRMServer"].ConnectionString);
    if (oMSCRMConn.IsReady) {
    //...
    }

    Here documentation:

    Use connection strings in XRM tooling to connect to Microsoft Dataverse (Dataverse) - Power Apps | Microsoft Docs

    If this was helpful, please check it as verified to help others finding useful information.

    Welcome to this amazing community.

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    Please refer to my sample code:

    gist.github.com/.../77f718d9a5802d4d087722d9e6a61d0a

    My sample will work without using registred app in AAD.

    Docs: "The AppId and RedirectUri provided above are examples of working application registration values. These values work everywhere our online services are deployed. However, they are provided here as examples and you are encouraged to create your own application registration in Azure Active Directory (Azure AD) for applications running in your tenant. Use your Username, Password, and Dataverse environment URL values in the connection string along with the RedirectUri and AppId you obtain from your Azure app registration."

    docs.microsoft.com/.../authenticate-office365-deprecation

    Good Luck !

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    HI,

    I have uploaded working console application code on my github repo -

    github.com/.../WorkWithCsharp

    Only thing you need to change is your CRM instance URL, Username and password in app.config file.

    I hope this helps!

    Please mark my answer verified if i were helpful

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 96 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans