I'm fairly new to D365 Finance & Operations and came across Microsoft's Service Samples on GitHub. Thought it was a good start to try and connect a simple .NET C# console application to D365. I know that this is a Dynamics AX Integration so if there's anything specific to D365 Finance & Operation, then please guide me to it.
I registered my D365 instance (not trial version) on Azure AD, created a secret key and also gave it permissions for Microsoft Dynamics ERP. I also registered Azure AD application on the D365 (System administration > Setup).
On my .NET solution I updated the configurations to match my configurations on Azure AD Tenant, Client/App ID and D365 URI.
When I run the console application I am successfully creating an authentication token of type bearer. The second I try to access data I am getting a 401 error. Here's a sample of request and response that I recorded using Fiddler.
GET https://xxxxx.sandbox.ax.dynamics.com/data/LegalEntities
HTTP/1.1
OData-Version: 4.0
OData-MaxVersion: 4.0
Accept: application/json;odata.metadata=minimal
Accept-Charset: UTF-8
User-Agent: Microsoft ADO.NET Data Services
Authorization: Bearer xxxxxxxxxx
Host: xxxxx.sandbox.ax.dynamics.com
HTTP/1.1 401 Unauthorized
Server: Microsoft-IIS/10.0
WWW-Authenticate: authorization_uri="https://login.windows.net/xxxxx/oauth2/authorize"
ms-dyn-fqhn: sms-dev-1-99efa75e-d745-4a64-9d27-a06f7b230253
ms-dyn-namespace: AxProdDevTest
ms-dyn-tenant: devtest-lcs-99efa75e-d745-4a64-9d27-a06f7b230253
ms-dyn-role: dev
ms-dyn-aid: 371cd53f-ee91-0004-d4f8-1e3791eed301
X-Powered-By: ASP.NET
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
p3p: CP="No P3P policy defined. Read the Microsoft privacy statement at https://go.microsoft.com/fwlink/?LinkId=271135"
Date: Mon, 11 Jun 2018 13:59:06 GMT
Content-Length: 0
I noticed that in the response it specifically says that P3P policy is not defined. Can someone shed some light on that please?
Moreover, is there some configurations that I'm missing? I am managing to get the token so the connection is there. It appears that I just don't have access to the data.
Thanks
UPDATE - Added source code I'm currently executing
string authHeader = string.Empty;
UriBuilder uri = new UriBuilder(ConfigurationManager.AppSettings["AzureUri"]);
AuthenticationContext authenticationContext = new AuthenticationContext(uri.ToString(), false);
string clientId = ConfigurationManager.AppSettings["ClientId"];
string clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
Task <AuthenticationResult> authenticationResult = authenticationContext.AcquireTokenAsync(ConfigurationManager.AppSettings["DynamicsUri"], clientCred);
authHeader = authenticationResult.Result.CreateAuthorizationHeader();
Uri dataUri = new Uri(ConfigurationManager.AppSettings["DynamicsUri"] + "/data", UriKind.Absolute);
var context = new Resources(dataUri);
context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(delegate (object sender, SendingRequest2EventArgs e) {
e.RequestMessage.SetHeader("Accept", "application/json");
e.RequestMessage.SetHeader("Authorization", authHeader);
});
foreach (var empEntity in context.Employees.Execute()){
Console.WriteLine("Name: {0}", empEntity.Name);
}