CRM supports various Deployment methods and unfortunately for the developers the method to connect to each of these differs. So here are the login functions for each of the Deployment methods supported by Dynamics CRM.
On Premise CRM connection:
On premise uses AD for authentication.
CrmService service = new CrmService();
service.Url = “http://” + servername + “/mscrmservices/2007/crmservice.asmx”;
service.CrmAuthenticationTokenValue.OrganizationName = “orgname”;
service.CrmAuthenticationTokenValue.AuthenticationType = 0;
service.Credentials = new System.Net.NetworkCredential(“username”, “Password”, “DomainName”);
OR
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
Live CRM connection:
CRM Live uses Passport authentication. It requires a reference to the Idcrlwrapper be added. The Idcrlwrapper is available along with the SDK as helper files.
Make sure you have copied the msidcrl40.dll to the system32 folder before you try to run this code.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.Url = “https://dev.crm.dynamics.com/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx“;
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);
// Retrieve a Windows Live ticket from the Live
service.LogonManager lm = new LogonManager();
string passportTicket = lm.Logon(“LiveWindowsUserId”, “password”, “crm.dynamics.com”, policyResponse.Policy, “Production”);
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.OrganizationName = “OrganizationName”;
crmTicketRequest.PassportTicket = passportTicket;
RetrieveCrmTicketResponse crmTicketResponse = (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest);
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 1;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;
Now, once you have the token assign it to the crmservice object as we do in on premise.
Hosted CRM connection:
This uses SPLA authentication. You need to use the userid and password provided to you by your hosting provider. You would also need to get the discovery URL from them.
From the discovery service URL you can retrieve the list of organizations.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.UserId = username;
orgRequest.Password = password;
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);
//Find the desired organization with which you want to connect.
//Retrieve the ticket.
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = organization;
ticketRequest.UserId = username;
ticketRequest.Password = password;
RetrieveCrmTicketResponse ticketResponse = (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);
//Create the CrmService Web service proxy.
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
sdktoken.OrganizationName = organization;
sdktoken.CrmTicket = ticketResponse.CrmTicket;
Now, once you have the token assign it to the crmservice object as we do in on premise.