CRM 2016 – Unable to Login to Dynamics CRM

Today I’ve been struggling with the Microsoft.Xrm libraries when connecting to Microsoft Dynamics CRM Online 2016.

My code is run from an ASP.NET web-site (4.6.1) – it worked locally, but when I pushed it to the QA server, it stopped working.

Basically this is my code:

var serviceClient = new CrmServiceClient(connectionString); 
var proxy = serviceClient.OrganizationWebProxyClient ?? (IOrganizationService)serviceClient.OrganizationServiceProxy;

Giving me the error: Unable to Login to Dynamics CRM

Based on some input I found here:

I setup some logging in web.config:

 <trace autoflush="true" />
 <source name="Microsoft.Xrm.Tooling.Connector.CrmServiceClient"
 <add name="console" type="System.Diagnostics.DefaultTraceListener" />
 <remove name="Default" />
 <add name ="fileListener" />
<source name="Microsoft.Xrm.Tooling.CrmConnectControl"
 <add name="console" type="System.Diagnostics.DefaultTraceListener" />
 <remove name="Default" />
 <add name ="fileListener" />
  <add name="Microsoft.Xrm.Tooling.Connector.CrmServiceClient" value="Verbose" />
 <add name="Microsoft.Xrm.Tooling.CrmConnectControl" value="Verbose" />
 <add name="Microsoft.Xrm.Tooling.WebResourceUtility" value="Verbose" />
 <add name="fileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="XRMToolingLogs.log" />

The error file states:

Microsoft.Xrm.Tooling.Connector.CrmServiceClient Error: 2 : Source : System.Security
Method : Protect
Date : 3/4/2016
Time : 1:57:34 PM
Error : The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.
Stack Trace : at System.Security.Cryptography.ProtectedData.Protect(Byte[] userData, Byte[] optionalEntropy, DataProtectionScope scope)
 at Microsoft.Xrm.Tooling.Connector.DeviceUserName.Encrypt(String value)
 at Microsoft.Xrm.Tooling.Connector.DeviceUserName.UpdateCredentials(String encryptedValue, String decryptedValue)
 at Microsoft.Xrm.Tooling.Connector.DeviceIdManager.GenerateDeviceUserName()
 at Microsoft.Xrm.Tooling.Connector.DeviceIdManager.GenerateDevice(String deviceName, String devicePassword)
 at Microsoft.Xrm.Tooling.Connector.DeviceIdManager.RegisterDevice(Guid applicationId, Uri issuerUri, String deviceName, String devicePassword)
 at Microsoft.Xrm.Tooling.Connector.DeviceIdManager.LoadOrRegisterDevice()
 at Microsoft.Xrm.Tooling.Connector.CrmWebSvc.GenerateDeviceCreds()
 at Microsoft.Xrm.Tooling.Connector.CrmWebSvc.InitCRM2011Service()
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Error: 2 : Unable to Login to Dynamics CRM
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Error: 2 : OrganizationWebProxyClient is null
Microsoft.Xrm.Tooling.Connector.CrmServiceClient Error: 2 : OrganizationServiceProxy is null


The fix:

Go to IIS, locate your AppPool, go to Advanced Settings and make sure that “Load User Profile” is set to true


If your site is an App Service hosted in Azure you need to modify the Application Settings within the Azure Portal to setup “Load User Profile”.

  1. Locate your web app / app service resource
  2. Hit the “Settings” button
  3. Locate “Application settings” – in the “General” category and hit that thing
  4. Scroll to “App settings” and create the following:
    2. Value: 1

Notice: This setting is not available in all App Service plans – you need to be on B1 or greater (remember that this requirement might be different at the time you read this post, as Microsoft continuously  changes the pricing and requirements of Azure – so test before upgrading)

Kategorier: Integration, Udvikling

Tagged as: ,

Skriv et svar

Udfyld dine oplysninger nedenfor eller klik på et ikon for at logge ind: Logo

Du kommenterer med din konto. Log Out /  Skift )

Google photo

Du kommenterer med din Google konto. Log Out /  Skift )

Twitter picture

Du kommenterer med din Twitter konto. Log Out /  Skift )

Facebook photo

Du kommenterer med din Facebook konto. Log Out /  Skift )

Connecting to %s