In one of my past articles, (Different ways to connect/authenticate Dynamics 365), I explained how to authenticate to your Dynamics environment using the connection string approach in a C# console application.
While this approach works well for non-MFA-enabled authentication, it does not work for environments where MFA is enabled.
In this article, lets explore how to authenticate to an MFA enabled environment. We will use the UX to prompt for authentication approach.
Steps to make in C# console application:
- In the App.config file, place a connectionstring as shown below.

<connectionStrings>
<add name="Xrm" connectionString="Url=https://rajeevtoolsdev.crm.dynamics.com;Username=rajeevpe@powertools.onmicrosoft.com;Integrated Security=true;authtype=OAuth;LoginPrompt=Auto;AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;
RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" />
</connectionStrings>
- Change only the Url and Username with your details. Leave the rest as is.
- In the Program.cs file place following code.
var connectionString = ConfigurationManager.ConnectionStrings["Xrm"].ConnectionString;
var crmConn = new CrmServiceClient(connectionString);
var reqWhoAmI = new WhoAmIRequest();
var resp = (WhoAmIResponse)crmConn.Execute(reqWhoAmI);
- Run the console, and you will see a login prompt as shown below. Complete the sign-in to authenticate to the environment.

🙂


![[Step by Step] Beginner : Create a PCF control and add it to a custom page](https://rajeevpentyala.com/wp-content/uploads/2024/12/image-49.png)
Leave a comment