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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

An error occured when processing the security tokens in the message: Authorization failed while connecting CRM online 365 9.0 from console

(0) ShareShare
ReportReport
Posted on by 2,667

Hi All,

I am not able to connect CRM online 365 9.0 from console application as its giving error An error occured when processing the security tokens in the message: Authorization failed 

What could be the issue ?

Thanks,

Jharana

I have the same question (0)
  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Any idea on above query ?

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

    Hello,

    Can you please share your code ? A screenshot will be useful to better understand the error.

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Mehdi,

    The below code am using :

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Client;

    using Microsoft.Crm.Sdk.Messages;

    using System.Net;

    using System.ServiceModel.Description;

    using Microsoft.Xrm.Sdk.Query;

    namespace ConnectCRMOnline

    {

       class Program

       {

           static void Main(string[] args)

           {

               //Create IOrgannization Service Object

               IOrganizationService service = ConnectD35OnlineUsingOrgSvc();

               if (service != null)

               {

                   Console.WriteLine("DO you want to see total account records? (y/n)");

                   var response = Console.ReadLine();

                   if (response != "y")

                       return;

                   //code your logic here

                   //Get count of contacts in the system

                   QueryExpression qe = new QueryExpression("account");

                   EntityCollection enColl = service.RetrieveMultiple(qe);

                   Console.WriteLine("Total account in the System=" + enColl.Entities.Count);

                   Console.ReadLine();

               }

           }

           public static IOrganizationService ConnectD35OnlineUsingOrgSvc()

           {

               IOrganizationService organizationService = null;

               String username = "";

               String password = "";

               String url = "";

               try

               {

                   ClientCredentials clientCredentials = new ClientCredentials();

                   clientCredentials.UserName.UserName = username;

                   clientCredentials.UserName.Password = password;

                   // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12

                   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                   organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri(url), null, clientCredentials, null);

                   if (organizationService != null)

                   {

                       Guid gOrgId = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).OrganizationId;

                       if (gOrgId != Guid.Empty)

                       {

                           Console.WriteLine("Connection Established Successfully...");

                       }

                   }

                   else

                   {

                       Console.WriteLine("Failed to Established Connection!!!");

                   }

               }

               catch (Exception ex)

               {

                   Console.WriteLine("Exception occured - " + ex.Message);

               }

               return organizationService;

           }

       }

    }

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

    Hello,

    Thank you for hiding your confidential data. Your username and password are visible on your last post.

    To correct your problem please use my sample code.

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Tooling.Connector;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.ServiceModel.Description;
    
    namespace ConnectCRMOnline
    {
        class Program
        {
            static void Main(string[] args)
            {
                CrmServiceClient crmSvc = getOrganizationService();
                Console.ReadLine();
            }
    
            public static CrmServiceClient getOrganizationService()
            {
                CrmServiceClient crmSvc = null;
                String connString = "AuthType=OAuth;Username=XXXX;Password=XXXX;Url=https://XXXS.api.crm4.dynamics.com;AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;TokenCacheStorePath=c:\\MyTokenCache;LoginPrompt=Auto";
                try
                {
                    crmSvc = new CrmServiceClient(connString);
                    if (crmSvc != null)
                    {
                        var whoAmIResponse = ((WhoAmIResponse)crmSvc.Execute(new WhoAmIRequest()));
                        if (whoAmIResponse != null)
                        {
                            Console.WriteLine("Connection OK....");
                            Console.WriteLine(whoAmIResponse.UserId);
    
                        }
                        else
                        {
                            Console.WriteLine("Connection KO....");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR...");
                    Console.WriteLine(e.Message);
                }
                return crmSvc;
            }
        }
    }
    

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Mehedi,

    Thnaks for ur above response . I have used ur code but here in conn string:  AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;TokenCacheStorePath=c:\\MyTokenCache;LoginPrompt=Auto";

    What i should use like Appid Redirect Uri Tokencachepath ?

    I have passed blank all those so getting error object not set to an instance of an object.

    Thankks,

    Jharana

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

    Hello,

    The AppId and RedirectUri provided above are examples of working application registration values. These values work everywhere.

    However, they are provided here as examples, and you are encouraged to create your own application registration in Azure Active Directory (AAD) for applications running in your tenant.

    Please refer to this documentation: https://docs.microsoft.com/en-us/powerapps/developer/data-platform/authenticate-office365-deprecation#what-should-i-do-to-fix-my-application-code-if-affected

    Please don't forget to verify my above answer if it was helpful, thank you.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 41 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 39

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans