I get this error

ID3242: The security token could not be authenticated or authorized

when I'm trying to connect to CRM 2016 on premise using the below code :

static IOrganizationService _service;

static void Main(string[] args)
{
           CRMConnect("myusername", "myPassword", "serviceURi");
           Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

           if (userid != Guid.Empty)
           {
               Console.WriteLine("Connection Established Successfully");
               Console.ReadKey();
           }
       }

       public static void CRMConnect(string UserName,string Password,string OrgServiceUri)
       {
           try
           {
               ClientCredentials credentials = new ClientCredentials();
               credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
               Uri serviceUri = new Uri(OrgServiceUri);
               OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
               proxy.EnableProxyTypes();
               _service = (IOrganizationService)proxy;
           }
           catch (Exception ex)
           {
               Console.WriteLine("Error while connecting to CRM " + ex.Message);
               Console.ReadKey();
           }
       }
   }
}

1 Answers

0
AnkUser On

How are you passing your Username?. Could you edit your answer with your username. Maybe Try to put @domain to your username.

Are you trying to connect to Microsoft Dynamics CRM from Scribe Insight and getting the error message “ID3242: The security token could not be authenticated or authorized”?

Error ID3242- Connecting Scribe to Dynamics CRM 1

I recently had this issue at a client and we spent some time trying to resolve it based on many other posts, most of which referred to Active Directory Federation Services (ADFS) configuration, audience permission settings and other suggestions. In our case, none of these things seemed to be the problem. Our connections were working fine a few days earlier and so we looked at what had changed. Turned out the certificates were about to expire and some work was happening related to that as well as an ADFS upgrade taking place. Previously, our connection to Dynamics CRM was defined with a domain level login like below:

Error ID3242- Connecting Scribe to Dynamics CRM 2

This was working fine but suddenly it failed with the ID3242 error message. My account was fine and I could still access Dynamics CRM through the browser just fine but Scribe would not connect for anyone. To resolve the issue it was a very simple change. It seems that with the upgrade of ADFS it now required the UPN (user principal name / MSDN – User Name Formats) login in order to authenticate. That is simply going from the domain login to the email address like this:

Error ID3242- Connecting Scribe to Dynamics CRM 3

Another option that worked was to use the Down-Level logon name (Domain\username)

Error ID3242- Connecting Scribe to Dynamics CRM 4

If you receive this error while trying to connect Scribe or anything else that consumes the Dynamics CRM services, give these suggestions a try and maybe you’ll be back up and running a little quicker. I hope this helps!

Refrence: https://community.dynamics.com/crm/b/dynamicscrmpros/archive/2016/03/21/how-to-solve-error-id3242-the-security-token-could-not-be-authenticated-or-authorized-when-connecting-scribe-to-microsoft-dynamics-crm