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