Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Cannot connect to Dynamics CRM Online instance

Posted on by Microsoft Employee

Hello All,

I'm new to CRM development and this is my first post here. I hope to resolve my issue with your help.

So, here's some details:

I created a sample app to connect to CRM online environment but I'm getting the bellow exception.

Value cannot be null. Parameter name: identityProvider


at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.AuthenticateOnlineFederationInternal(AuthenticationCredentials authenticationCredentials) at Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1.Authenticate(AuthenticationCredentials authenticationCredentials) at SampleApplication.Controllers.HomeController.GetProxy[TService,TProxy](IServiceManagement`1 serviceManagement, AuthenticationCredentials authCredentials) at SampleApplication.Controllers.HomeController.Index()

The strange thing is that it is working as expected on one of the servers we have, but throws the exception I mentioned when accessed from the other. Both machines are running Windows Server 2012 with Windows Identity Foundation 3.5 installed. I'm using Microsoft.XRM.SDK v8.0.0.0 with .NET 4.5.2. Here's the code block:

var orgServiceManagement = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://************.crm4.dynamics.com/XRMServices/2011/Organization.svc"));
var endpointType = orgServiceManagement.AuthenticationType;

var authCredentials = GetCredentials(endpointType, "*******@********.onmicrosoft.com", "**********");

var organizationProxy = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceManagement, authCredentials);

organizationProxy.EnableProxyTypes();
organizationProxy.Authenticate();

//rest of the code...


And here's the essential part of the two used methods:

private static AuthenticationCredentials GetCredentials(AuthenticationProviderType endpointType, string username, string pass)
{
    var authCredentials = new AuthenticationCredentials();

    authCredentials.ClientCredentials.UserName.UserName = username;
    authCredentials.ClientCredentials.UserName.Password = pass;
    authCredentials.SupportingCredentials = new AuthenticationCredentials();
    authCredentials.SupportingCredentials.ClientCredentials = Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();

    return authCredentials;
}

private static TProxy GetProxy<TService, TProxy>(IServiceManagement<TService> serviceManagement, AuthenticationCredentials authCredentials)
where TService : class
where TProxy : ServiceProxy<TService>
{
    Type classType = typeof(TProxy); 
    if (serviceManagement.AuthenticationType != AuthenticationProviderType.ActiveDirectory)
    {
        var tokenCredentials = serviceManagement.Authenticate(authCredentials);
        return (TProxy)classType
                      .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
                      .Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
    } 

    //other staff here
}

I was adviced to use the new and simpler approach with Microsoft.Xrm.Tooling.Connector.CrmServiceClient but the result was almost the same:

Object reference not set to an instance of an object.

at Microsoft.Xrm.Tooling.Connector.CrmServiceClient.Execute(OrganizationRequest request) 

I would appreciate any help! Thanks!

EDIT: Here's the simplified connection code and connectionString

try
{
	CrmServiceClient service = new CrmServiceClient(ConfigurationManager.ConnectionStrings["Crm"].ConnectionString);
	var orgService = (IOrganizationService)service.OrganizationWebProxyClient != null ? 
							 (IOrganizationService)service.OrganizationWebProxyClient : 
							 (IOrganizationService)service.OrganizationServiceProxy;

	var whoAmI = orgService.Execute(new WhoAmIRequest());	
}
catch (Exception ex)
{
	//Error handling goes here
}


 <add name="Crm" connectionString="AuthType=Office365;Url=https://******.crm4.dynamics.com;Username=*****;Password=****" />


*This post is locked for comments

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    Nadeeja Bomiriya 6,804 on at
    RE: Cannot connect to Dynamics CRM Online instance

    Hi Yordan,

    Please try one of the appropriate constructors provided in this article.

    msdn.microsoft.com/.../dn688177.aspx

    CrmServiceClient crmSvc = new CrmServiceClient("<UserName>", CrmServiceClient.MakeSecureString("<Password>"), "<CrmRegion>", "<OrgName>", useUniqueInstance:false, useSsl:false, <orgDetail>, isOffice365:true);

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Cannot connect to Dynamics CRM Online instance

    Please see my edited question.

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: Cannot connect to Dynamics CRM Online instance

    Please share the code for your simplified connection with connection string you are using (just don't paste passwords and usernames)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Cannot connect to Dynamics CRM Online instance

    Hi Nadeeja,

    Thank you for the response. I checked the HTTP Activation feature and it is enabled. The other features are identical. Any other ideas what might be causing this strange behavior?

    Thanks,

    Yordan

  • Suggested answer
    Nadeeja Bomiriya Profile Picture
    Nadeeja Bomiriya 6,804 on at
    RE: Cannot connect to Dynamics CRM Online instance

    Hi Yordan,

    On the machine that throws the error, check if .Net Framework 4.5 > WCF Services > HTTP Activation is enabled under features?

    Also, it would be a good idea to compare the other features in both working and not working servers.

    Cheers,

    Nadeeja

    If the answer solves your problem, please mark as Verified. Thanks.

    My Blog: http://dyn365apps.com/ - Follow me on Twitter: https://twitter.com/dyn365apps

    LinkedIn: https://www.linkedin.com/in/nadeeja

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans