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,...
Answered

Cannot connect my console app to my dynamics 365 sales instance

(0) ShareShare
ReportReport
Posted on by 5

Hello everyone,

I am new to dynamics 365 and I have recently tried to develop a console job to manipulate opportunities on dynamics 365 platform, however, I am not able to connect.

It gives me this error when I run the app:

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

Part of the code for connection:

IOrganizationService service;

ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = "****@******onmicrosoft.com";
credentials.UserName.Password = "*************";

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

service = new OrganizationServiceProxy(new Uri("https://**************************************.svc"), null, credentials, null);

if(service != null)
{

      Guid userID = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;


      if(userID != Guid.Empty)
      {
          Console.WriteLine("Connection successful");
      }

}

else
{
     Console.WriteLine("FAILED");
}

Here is a screenshot

1157.png

I have tried adjusting the time zone and updating the NuGet packages but the error persists.

Thanks.

I have the same question (0)
  • Verified answer
    Clofly Profile Picture
    260 on at

    Hi Prithvi647,

    The class ClientCredentials you are using is deprecated to connect to Dynamics 365.(It is WS-Trust authentication method.)

    Instead, the current class we should use is CrmServiceClient.(Office 365 authentication method.)

    Here is an article about introduction to the new connection method.

    https://xrmdynamicscrm.wordpress.com/2020/09/11/office365-authentication-type-and-organizationserviceproxy-class-for-connecting-to-common-data-service-deprecated/

    Firstly, we need to register an Azure AD app and grant its access to Dynamics CRM, you can follow this tutorial to set up the app.

    https://carldesouza.com/calling-the-power-apps-and-dynamics-365-web-api-from-the-postman-windows-client/

    Please refer to my code, we use connection string to connect to D365.

    (AppId is our Azure AD app. and I added a custom redirect URL.)

    rdl.JPG

    static void Main(string[] args)
    {
    try
    {
    
        string ConnectionString = "AuthType = OAuth; "  
        "Username = admin@crm123456.onmicrosoft.com;"  
        "Password = 123456; "  
        "Url = https://org123456.crm5.dynamics.com;"  
        "AppId=xxxxxxxx-b283-4644-8a7f-b72d0bffc9e0;"  
        "RedirectUri=https://asyouwish;"  
        "LoginPrompt=Auto";
    
        Console.WriteLine("Waiting for connection...");
    
        CrmServiceClient svc = new CrmServiceClient(ConnectionString);
    
        if (svc.IsReady)
        {
            Console.WriteLine("Ready!");
            IOrganizationService orgService;
            orgService = (IOrganizationService)svc.OrganizationWebProxyClient != null ? (IOrganizationService)svc.OrganizationWebProxyClient : (IOrganizationService)svc.OrganizationServiceProxy;
            Guid userID = ((WhoAmIResponse)orgService.Execute(new WhoAmIRequest())).UserId;
    
    
            if (userID != Guid.Empty)
            {
                Console.WriteLine("Connection successful"   "Your GUID is: "   userID);
            }
    
        }
    
    }
    
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
    
    Console.ReadLine();
    
    }

    Run the code to test

    7462.test.JPG

  • Suggested answer
    Prithvi647 Profile Picture
    5 on at

    Thank you you solution worked great.

  • Clofly Profile Picture
    260 on at

    Hi Prithvi647,

    It is very glad that the solution works for you.

    If you had found any answer helped, please kindly mark it as verified to close the thread, which would help community users who have similar question, thanks. :)

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Prithvi647,

    Please ignore this reply.

    Clofly 2 Mao is my backup account, there is issue with my main account in recent days, now the issue is fixed, this is just a simple reply from my main account.

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

#2
Gerardo Rentería García Profile Picture

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

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans