Connecting to Dynamics 365 Environment From C# Application

In this blog post, we’ll securely connect to a Dynamics 365 environment using ClientID.

Before we start

Register an application in Azure Active Directory (Azure AD) to obtain a ClientID (Application ID). These credentials will be used for authenticating your application.


public void Connect()
{
string connString = $"AuthType=OAuth;" +
$"Username={your@username};" + // Your Azure AD username
$"Password={yourpassword};" + // Your Azure AD password
$"Url=https://myenv.crm4.dynamics.com/;" + // Replace with your Dynamics 365 environment URL
$"AppId={your_app_id_in_azure_ad};" + // Your Azure AD Application ClientID
$"RedirectUri=app://{your_app_id_in_azure_ad};" + // Your Azure AD Application Redirect URI
$"LoginPrompt=Never";
if (string.IsNullOrWhiteSpace(connString))
{
Console.WriteLine("No Connection string entered!");
return;
}
var client = new CrmServiceClient(connString);
var orgService = (IOrganizationService)client;
}
view raw Connect.cs hosted with ❤ by GitHub
  • AuthType is set to OAuth to indicate OAuth authentication.
  • Replace {your@username} and {yourpassword} with your credentials.
  • Make sure to replace the Url with the correct URL of your Dynamics 365 environment.
  • {your_app_id_in_azure_ad} should be your Azure AD Application’s ClientID.
  • The RedirectUri should also be updated with the Redirect URI registered in your Azure AD Application

Leave a comment