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:

https://github.com/MscrmTools/XrmToolBox/issues/299

I setup some logging in web.config:

<system.diagnostics>
 <trace autoflush="true" />
 <sources>
 <source name="Microsoft.Xrm.Tooling.Connector.CrmServiceClient"
 switchName="Microsoft.Xrm.Tooling.Connector.CrmServiceClient"
 switchType="System.Diagnostics.SourceSwitch">
 <listeners>
 <add name="console" type="System.Diagnostics.DefaultTraceListener" />
 <remove name="Default" />
 <add name ="fileListener" />
 </listeners>
 </source>
<source name="Microsoft.Xrm.Tooling.CrmConnectControl"
 switchName="Microsoft.Xrm.Tooling.CrmConnectControl"
 switchType="System.Diagnostics.SourceSwitch">
 <listeners>
 <add name="console" type="System.Diagnostics.DefaultTraceListener" />
 <remove name="Default" />
 <add name ="fileListener" />
 </listeners>
 </source>
 </sources>
 <switches>
  <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" />
 </switches>
 <sharedListeners>
 <add name="fileListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="XRMToolingLogs.log" />
 </sharedListeners>
</system.diagnostics>

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

Update

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:
    1. Name: WEBSITE_LOAD_USER_PROFILE
    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: ,