Dynamics AX 2012 中的 API

问题描述 投票:0回答:1

我在 AX 2012 中遇到问题,当我通过 Https URL 调用 API 时出现此错误

(请求被中止:无法创建 SSL/TLS 安全通道)

但是当我在 Dynamic 365 中测试代码时,它可以工作了

这是我的代码:

str destinationUrl,requestJson, responseJson;

System.Net.HttpWebRequest  request;

System.Net.HttpWebResponse  response = new System.Net.HttpWebResponse();

    CLRObject      clrObj;

System.Byte[]         bytes;

System.Text.Encoding     utf8;

System.IO.Stream       requestStream, responseStream;

System.IO.StreamReader    streamReader;

System.Exception        ex;

System.Net.WebHeaderCollection httpHeader;

System.Net.ServicePoint    servicePt;
System.Net.ServicePointManager ServicePointManager;

str           byteStr;

System.Byte[]      byteArray;

System.IO.Stream     stream;

System.IO.Stream     dataStream;
int secProtocol;
boolean ssl3,tls,tls11,tls12,tls13;

destinationUrl = 'https://...';

requestJson  = @'MyJsonData';
try

{

  new InteropPermission(InteropKind::ClrInterop).assert();

  httpHeader = new System.Net.WebHeaderCollection();

  clrObj = System.Net.WebRequest::Create(destinationUrl);
   
  request = clrObj;

  utf8 = System.Text.Encoding::get_UTF8();

  bytes = utf8.GetBytes(requestJson);

   request.set_Method("POST");

  httpHeader.Add("Client-Id","5...");
  httpHeader.Add("Secret-Key","...");
  httpHeader.Add("Cookie", "...");

  request.set_Headers(httpHeader);

  request.set_ContentType('application/json');

  request.set_ContentLength(bytes.get_Length());
 
  //ServicePointManager.Expect100Continue = true;
    System.Net.ServicePointManager::set_Expect100Continue(true);
  //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);
  secProtocol = System.Net.ServicePointManager::get_SecurityProtocol();

  ssl3 = secProtocol == enum2int(System.Net.SecurityProtocolType::Ssl3);
  tls = secProtocol == enum2int(System.Net.SecurityProtocolType::Tls);
  tls11 = secProtocol == enum2int(System.Net.SecurityProtocolType::Tls11);
  tls12 = secProtocol == enum2int(System.Net.SecurityProtocolType::Tls12);
  tls13 = secProtocol == 12288;
   
  info(strFmt("SSL3 enabled: '%1' | TLS enabled: '%2' | TLS1.1 enabled: '%3' | TLS1.2 enabled: '%4' | TLS1.3 enabled: '%5'", ssl3, tls, tls11, tls12, tls13));
   
  requestStream = request.GetRequestStream();

  requestStream.Write(bytes, 0, bytes.get_Length());

  response = request.GetResponse();

  responseStream = response.GetResponseStream();

  streamReader = new System.IO.StreamReader(responseStream);

  responseJson = streamReader.ReadToEnd();

  info(responseJson);

}

catch (Exception::CLRError)

{

  ex = CLRInterop::getLastException().GetBaseException();

  error(ex.get_Message());

}

我用过这个: System.Net.ServicePointManager::set_Expect100Continue(true); System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Tls12);

dynamic-programming dynamics-ax-2012 dynamics-ax-2012-r2 dynamic365
1个回答
0
投票

AX 2012 是一个旧应用程序,看起来存在 TLS 问题。

最好的做法可能是将您的代码移至C#

包装器,然后从 AX 调用您的包装器。这将确保使用您的 TLS 设置。

否则,请尝试通过修改 AX 配置文件强制使用 TLS。如果你的代码正在运行

  • 服务器端 - [

    在 AOS 上] C:\Program Files\Microsoft Dynamics AX\60\Server\[YourAOSFolder]\bin\Ax32Serv.exe.config

    
    

  • 客户端 -

    C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\Ax32.exe.config

    
    

我不是 TLS 专家,只是对 .Net 应用程序进行了一些谷歌搜索,您需要添加如下内容之一。就我个人而言,我只是对它们进行试验,您可能需要重新启动 AOS 或 AX 客户端,具体取决于您放置代码的位置。 TLS 问题可能会很麻烦,所以甚至可能是服务器问题,但我不这么认为。

样品 1 -

<runtime> <AppContextSwitchOverrides value="Switch.System.ServiceModel.DisableUsingServicePointManagerSecurityProtocols=false;Switch.System.Net.DontEnableSchUseStrongCrypto=false"/> </runtime>
样品 2 -

<runtime> <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSystemDefaultTlsVersions=false"/> </runtime>
样品 3 -

<runtime> <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=false"/> </runtime>
    
© www.soinside.com 2019 - 2024. All rights reserved.