Saturday, October 12, 2019

D365FO - Data integration by OData (Part 3 of 5)

D365FO - Data integration by OData (Part 1 of 5)
D365FO - Data integration by OData (Part 2 of 5)
D365FO - Data integration by OData (Part 3 of 5) You are here!
D365FO - Data integration by OData (Part 4 of 5)
D365FO - Data integration by OData (Part 5 of 5)


Create OData client application


There are 3 main steps:
  1. Create AuthenticationUtility (C# class library)
  2. Create ODataUtitlity (C# console application)
  3. Create OData client (C# console application)

Create AuthenticationUtility

First, we create a C# class library project. Name it as 'AuthenticationUtility'.































Next, use 'Manage NuGet Packages...' to add the following reference.
  • Microsoft.IdentityModel.Clients.ActiveDirectory

**Some references will be used in several projects, so make sure the references version consistent on all projects.






















Then, add a 'ClientConfiguration' class into the project with the following code.

Check and change Tenant and Client App ID.


































 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace AuthenticationUtility  
 {  
   public partial class ClientConfiguration  
   {  
     public static ClientConfiguration Default  
     {  
       get  
       {  
         return ClientConfiguration.OneBox;  
       }  
     }  
     public static ClientConfiguration OneBox = new ClientConfiguration()  
     {  
       UriString         = "https://usnconeboxax1aos.cloud.onebox.dynamics.com/",  
       ActiveDirectoryResource  = "https://usnconeboxax1aos.cloud.onebox.dynamics.com",  
       ActiveDirectoryTenant   = "https://login.windows.net/*****.onmicrosoft.com",  
       ActiveDirectoryClientAppId = "*****",  
     };  
     public string UriString { get; set; }  
     public string ActiveDirectoryResource { get; set; }  
     public String ActiveDirectoryTenant { get; set; }  
     public String ActiveDirectoryClientAppId { get; set; }  
   }  
 }  



Next, add a 'OAuthHelper' class into the project with the following code.

Here we use the URI which already registered in AAD the previous blog.


 using Microsoft.IdentityModel.Clients.ActiveDirectory;  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace AuthenticaitionUtility  
 {  
   public class OAuthHelper  
   {  
     /// <summary>  
     /// The header to use for OAuth.  
     /// </summary>  
     public const string OAuthHeader = "Authorization";  
     /// <summary>  
     /// retrieves an authentication header from the service.  
     /// </summary>  
     /// <returns>the authentication header for the Web API call.</returns>  
     public static string GetAuthenticationHeader(bool useWebAppAuthentication = false)  
     {  
       string aadTenant   = ClientConfiguration.Default.ActiveDirectoryTenant;  
       string aadClientAppId = ClientConfiguration.Default.ActiveDirectoryClientAppId;      
       string aadResource  = ClientConfiguration.Default.ActiveDirectoryResource;  
       AuthenticationResult authenticationResult;  
       var authenticationContext = new AuthenticationContext(aadTenant,  
                                  TokenCache.DefaultShared);  
       authenticationResult = authenticationContext.AcquireTokenAsync(aadResource,   
                                       aadClientAppId,  
                                       new Uri("https://MyCar"),  
                                       new PlatformParameters(PromptBehavior.Auto)).Result;  
       // Create and get JWT token  
       return authenticationResult.CreateAuthorizationHeader();  
     }  
   }  
 }  




After all, build the project. We should get the good result with AuthenticationUtitlity.dll which will be used by OData client in the further step.









=== End of Part 3 ===

1 comment: