As a developer, I regularly create connection to my D365 instance (On-Premise) and/or D365 CDS online(MFA and non-MFA) programmatically using c#. I will list down how you can create connection strings for the same below.
- D365 CDS Online (Non-MFA)
var connectionString = @"AuthType = Office365; Url = https://[org].crm4.dynamics.com/; Username=[username]; Password=[password]"; | |
CrmServiceClient conn = new CrmServiceClient(connectionString); | |
IOrganizationService service; | |
service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy; |
Note: As per Microsoft announcement, Use of the WS-Trust authentication security protocol when connecting to Common Data Service is no longer recommended and has been deprecated; see the announcement.
This change only impacts client applications that connect to the Common Data Service. It does not impact custom plug-ins, workflow activities, or on-premises/IFD service connections.
Please check my blog post, “Connect to Multi-factor Enabled D365 CDS Programmatically” for more details.
- D365 CE (On-Premise)
Method 1
public IOrganizationService GetCRMService(ref IOrganizationService _service) | |
{ | |
if (_service == null) | |
{ | |
Uri organizationServiceUri = new Uri(@CRMOrganizationUri); | |
IServiceConfiguration<IOrganizationService> serviceConfiguration = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(organizationServiceUri); | |
System.ServiceModel.Description.ClientCredentials clientCredentials = new System.ServiceModel.Description.ClientCredentials(); | |
clientCredentials.UserName.UserName = <UserName>; | |
clientCredentials.UserName.Password = <Password>; | |
using (OrganizationServiceProxy organizationServiceProxy = new OrganizationServiceProxy(serviceConfiguration, clientCredentials)) | |
{ | |
_service = (IOrganizationService)organizationServiceProxy; | |
} | |
} | |
return _service; | |
} |
Method 2 (On-Premise without IFD)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; | |
string connectionString = @"AuthType = AD; Url = <URL>;Domain=<domain>;Username=<UserName>;Password=<Password>"; | |
CrmServiceClient conn = new CrmServiceClient(connectionString); | |
IOrganizationService service; | |
service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy; | |
Hope this helps to achieve your goal. Please share your comments.