【发布时间】:2022-01-03 05:07:09
【问题描述】:
我需要从 C# Windows 窗体应用程序连接到本地 D365 FO。
到目前为止,我创建了一个 Azure 帐户并注册了一个应用程序,所以现在我有了“应用程序(客户端)ID”、“目录(租户)ID”并创建了一个客户端密码。
我需要做什么才能使用数据管理包 REST API 连接到 D365 FO?
【问题讨论】:
标签: dynamics-365 dynamics-365-operations
我需要从 C# Windows 窗体应用程序连接到本地 D365 FO。
到目前为止,我创建了一个 Azure 帐户并注册了一个应用程序,所以现在我有了“应用程序(客户端)ID”、“目录(租户)ID”并创建了一个客户端密码。
我需要做什么才能使用数据管理包 REST API 连接到 D365 FO?
【问题讨论】:
标签: dynamics-365 dynamics-365-operations
查看Authorization Helper,它是Microsoft 为数据管理API 提供的示例控制台应用程序的一部分(参见https://docs.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/data-entities/data-management-api 中的最后一句)。应用程序的Program.cs 显示了如何使用身份验证助手。
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();
}
}
}
程序.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();
}
}
}
【讨论】: