Connect to D365 CE Programmatically without MFA/ On-Premise D365 CE

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.

Like this article?

Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Pinterest

Let Nemely  keep you updated!

Questions, comments, or suggestions? Contact us, and we’ll get back to you as soon as possible.

Related Posts

Webinar: Work More Efficiently with Copilot and AI in Microsoft Dynamics

Webinar: Work More Efficiently with Copilot and AI in Microsoft Dynamics

In the rapidly evolving world of technology, artificial intelligence (AI) continues to play a significant role in…
Connect to Multi-factor Enabled D365 Dataverse/CDS Programmatically Using Certificates (Online 9.1)

Connect to Multi-factor Enabled D365 Dataverse/CDS Programmatically Using Certificates (Online 9.1)

As the continuation to my earlier blog post, “Connect MFA Enabled D365 CDS Programamatically”, where we connected…
Generate PDF Report in Dynamics CRM 365

Generate PDF Report in Dynamics CRM 365

There is plenty of material available to achieve this very commonly needed functionality to generate PDF of…

Read More About Our Products

crm alerts hero
AppIcon_Colorful
CRM Alerts
The ultimate companion app for your mobile CRM users. CRM Alerts simplifies the work life for any CRM user that is doing sales-related activities with their mobile devices on a regular basis
shutterstock_710149132
NemelyProspectingHero
Product Lifecycle Management
shutterstock_1233205321
Nemely Essentials
Legal Practice Management