Console Application code to connect to Dynamics 365 on-premise, follow the below step-by-step process to connect, using AuthType as IFD
1. Open Visual Studio -> File -> New -> Project -> Search & Select C# Console Application (.Net Framework) -> Click Next
2. Give a suitable name for your project -> Click Create
Click on Refernces -> Manage NuGet Packages -> Search for Microsoft.Xrm.Tooling.CoreAssembly and install it
4. Add below references to your program.cs file
6. Below is the complete code:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Net;
using System.ServiceModel.Description;
using Microsoft.Xrm.Tooling.Connector;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
IOrganizationService oServiceProxy;
//Create the Dynamics 365 Connection:
CrmServiceClient oMSCRMConn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient("Domain=XXXX; AuthType=IFD;Username=XXXXX;Password=XXXXX;URL=XXXXXXX;");
//Create the IOrganizationService:
oServiceProxy = (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient != null ? (IOrganizationService)oMSCRMConn.OrganizationWebProxyClient : (IOrganizationService)oMSCRMConn.OrganizationServiceProxy;
if (oServiceProxy != null)
{
//Get the current user ID:
Guid userid = ((WhoAmIResponse)oServiceProxy.Execute(new WhoAmIRequest())).UserId;
if (userid != Guid.Empty)
{
Console.WriteLine("Connection Successful!");
}
}
else
{
Console.WriteLine("Connection failed...");
}
}
}
}
Thank you!!!
Happy CRMing...!!!