1

I need to make a connection from C# Windows Forms application to an on premise D365 FO.

So far, I created an Azure account and registered an application, so by now I have "Application (client) ID", "Directory (tenant) ID" and created a Client Secret.

What do I need to do to connect to D365 FO by using Data management package REST API?

FH-Inway
  • 4,432
  • 1
  • 20
  • 37
Alexander
  • 139
  • 8

1 Answers1

0

Take a look at the Authorization Helper, which is part of the sample console app provided by Microsoft for the data management api (see the last sentence in https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/data-management-api). The Program.cs of the app shows how the Authentication Helper is used.

AuthorizationHelper.cs

using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorizationHelper
{
    public class AuthorizationHelper
    {
        const string aadTenant = "https://login.windows.net/<your-tenant>";
        public const string aadResource = "https://<yourAOS>.cloudax.dynamics.com";
        const string aadClientAppId = "<client id>";        
        const string aadClientAppSecret = "<client secret>";

        /// <summary>
        /// Retrieves an authentication header from the service.
        /// </summary>
        /// <returns>The authentication header for the Web API call.</returns>
        public static string GetAuthenticationHeader()
        {
            AuthenticationContext authenticationContext = new AuthenticationContext(aadTenant);
            AuthenticationResult authenticationResult;
            
            var creadential = new ClientCredential(aadClientAppId, aadClientAppSecret);
            authenticationResult = authenticationContext.AcquireTokenAsync(aadResource, creadential).Result;
            
            // Create and get JWT token
            return authenticationResult.CreateAuthorizationHeader();
        }
    }
}

Program.cs

using ODataClient.Microsoft.Dynamics.DataEntities;
using System;

namespace DataPackageHandler
{
    using AuthorizationHelper;
    using Microsoft.OData.Client;

    class Program
    {
        static void Main(string[] args)
        {
            string ODataEntityPath = AuthorizationHelper.aadResource + "/data";
            Uri oDataUri = new Uri(ODataEntityPath, UriKind.Absolute);

            var d365Client = new Resources(oDataUri);
            d365Client.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(delegate (object sender, SendingRequest2EventArgs e)
            {
                var authenticationHeader = AuthorizationHelper.GetAuthenticationHeader();
                e.RequestMessage.SetHeader("Authorization", authenticationHeader);
            });

            PackageImporter.ImportPackage(d365Client, @"..\debug\SampleData\usmf_asset-major-types-01.zip");
            PackageExporter.ExportPackage(d365Client, @"..\debug\SampleData\");

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }        
    }
}
FH-Inway
  • 4,432
  • 1
  • 20
  • 37
  • Thank you FH. The offered solution seems to be based on OData. https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/integration-overview says "For on premise deployments, the only supported API is the Data management package REST API". Can this approach still work in a Windows Forms application that could be running in both interactive and scheduled fully-automated mode? – Alexander Jan 10 '22 at 03:27
  • Also, I could not find Microsoft.OData.Client, Microsoft.Dynamics.DataEntities and Microsoft.IdentityModel.Clients.ActiveDirectory - please advise on what needs to be installed on my desktop. Thank you. – Alexander Jan 10 '22 at 04:18
  • I suggest you familiarize yourself with the general requirements for D365 FO development: https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-tools/developer-home-page If you have further questions, please create a new question. – FH-Inway Jan 10 '22 at 15:44
  • FH, thank you for the reference, it was helpful. There are two main issues: many of the old code samples refer to legacy ADAL authentication rather than the current MSAL, and a number of methods changed their signatures over time. A constructor for AuthenticationContext used to take parameter, but not anymore., Microsoft.IdentityModel.Clients.ActiveDirectory referenced in AuthorizationHelper class seems to have a newer version of Microsoft.Identity.Client. I could not find a current package to make Microsoft.IdentityModel.Clients.ActiveDirectory recognized. Any advice will be appreciated. – Alexander Jan 17 '22 at 04:50