lunedì 26 febbraio 2024

AX 2012: The request was aborted: Could not create SSL/TLS secure channel

The error you're encountering, "The request was aborted: Could not create SSL/TLS secure channel," can occur due to various reasons when working with certificates and TLS in .NET environments. Here are some troubleshooting steps and solutions you can consider to resolve this issue:

1. Ensure the Certificate is Correctly Installed

  • Correct Store Location: Make sure the certificate is installed in the correct store and location. You mentioned using StoreLocation.LocalMachine; ensure that the certificate is indeed there and not mistakenly placed in StoreLocation.CurrentUser.

  • Permissions: The account under which your application runs might not have permissions to access the certificate from the LocalMachine store. You might need to grant the appropriate permissions to the account for the certificate. This is especially relevant for web applications running under specific service accounts.
  1. Find the Certificate in MMC:

    • Open the Microsoft Management Console (MMC) by pressing Win + R, typing mmc, and pressing Enter.
    • Add the Certificate Snap-in for the Local Computer account.
    • Navigate to the Personal/Certificates folder and find your certificate.
  2. Manage Private Key Permissions:

    • Right-click on the certificate, go to All Tasks > Manage Private Keys.
    • This opens a permission dialog where you can add the user account under which your application runs.
    • Grant at least Read permission to the account. For web applications, this is often the application pool identity, such as IIS AppPool\YourAppPoolName for IIS-hosted apps.

2. Use the Correct Certificate

Ensure you are loading the correct certificate by checking its thumbprint or subject name. It's easy to load the wrong certificate if not careful.

3. Enable TLS 1.2 in Your Application

If your application does not explicitly enable TLS 1.2, it might attempt to use an older, less secure protocol. You can enforce TLS 1.2 with the following line of code:

csharp
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

Place this line at the start of your application, before making any requests. This ensures that your application explicitly uses TLS 1.2 for its secure connections.

4. Check Certificate Chain and Expiry

  • Certificate Chain: Ensure that the entire certificate chain is trusted by the machine. Sometimes, intermediate certificates are missing or not correctly installed.
  • Expiry: Check if the certificate or any certificate in the chain has not expired.

5. Debugging SSL/TLS Issues

  • Logging: Use logging to capture more details about the failure. .NET can provide detailed logs that can help pinpoint the issue.
  • Network Monitoring Tools: Tools like Wireshark can help you see the TLS handshake and where it might be failing.
  • Microsoft Management Console (MMC): Use MMC to inspect the certificates installed on the machine to ensure they are correctly installed and have the necessary private keys.

6. Application Pool Identity (For Web Applications)

If you're developing a web application, ensure that the application pool identity has access to the certificate. This can be an issue when certificates are stored in the LocalMachine store.

7. Update .NET Framework

Ensure you're using a version of the .NET Framework that supports TLS 1.2 fully and has the latest security patches. Sometimes, simply updating .NET can resolve these issues.

martedì 2 febbraio 2021

D365FFO: Export data entity by Code X++

A simple way to export a data entity, using the powerful data management framework.

You need a definition group, where the entity is mapped and all parameters are set, this script allow you to simple get an output file. You can set or not the pushing method, in my case i need an option to force full puch when a entity is mapped using incremental pushing.


DMFDefinitionGroupName definitionGroupName;
DMFEntityName entityName;
DMFSourceName sourceName;
DMFExecutionId executionid = "..."; //Your unique execution identifier
DMFDefinitionGroup definitionGroup = DMFDefinitionGroup::find(definitionGroupName, true);

DMFEntityExporter exporter = new DMFEntityExporter();
Description description = "..."

DMFDefinitionGroupEntity definitionGroupEntity = DMFDefinitionGroupEntity::find(
	definitionGroupName",
	entityName,
	true);
	
DMFDefinitionGroupExecution::serviceInsertOrDisplay(definitionGroup,
	executionid,
	definitionGroupEntity.Entity,
	definitionGroupEntity.SampleFilePath,
	definitionGroup.Description,
	'',
	'',
	'',
	NoYes::Yes,
	DMFFileType::File,
	1,
	'',
	false,
	curExt());

DMFDefinitionGroupExecution definitionGroupExecution = DMFDefinitionGroupExecution::find(
	definitionGroupName,
	entityName,
	executionid,
	true);

ttsBegin;
definitionGroupExecution.selectForUpdate(true);
definitionGroupExecution.ExecuteTargetStep = NoYes::Yes;
if (...) //Full push condition
{
	definitionGroupExecution.DefaultRefreshType = DMFRefreshType::FullPush;
}
definitionGroupExecution.Update();
ttsCommit;

DataImportFramework::MoveToStaging(executionid);

SharedServiceUnitFileID fileId = DMFPackageExporter::exportToFileV2(
	definitionGroupName,
	executionid,
	entityName,
	sourceName);

mercoledì 27 gennaio 2021

D365FFO: Start a Virtual Machine (Cloud hosted) using Powershell

The simplest way to wake up and start a cloud hosted virtual machine from your commmand line.

The authentication method is prompted login, but you can autheticate using another method as mentioned here: https://docs.microsoft.com/en-us/powershell/module/azurerm.profile/connect-azurermaccount?view=azurermps-6.13.0

1. Run powersheel

2. Connect-AzureRmAccount

3. Start-AzureRmVM -ResourceGroupName "yourresourcegroupname" -Name "maschineName"

More info here: https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/?view=azurermps-6.13.0#virtual-machines

venerdì 22 gennaio 2021

D365FFO: Export data entity by code X++

 This is the code for exporting data entity using X++

Note: Export is done using batch job, consider it in case of used that on a performance requirement implementation

try
{
EntityName entityName = DMFEntity::findFirstByTableId(tableNum(VendVendorV2Entity)).EntityName;

Query query = new Query(DMFUtil::getDefaultQueryForEntity(entityName));
QueryBuildDataSource qbds = query.dataSourceTable(tableNum(BankPositivePayExportEntity));
DMFEntityExporter exporter = new DMFEntityExporter();
fileId = exporter.exportToFile(entityName,
definitionGroupName,
'', //Optional: ExecutionID
"CSV", //Optional::SourceName
#FieldGroupName_AllFields, //Optional field selection
query.pack(), //Optional: Filtered Query
curExt() //Optional: DataAReaId
);

if (fileId != '')
{
str downloadUrl = DMFDataPopulation::getAzureBlobReadUrl(str2Guid(fileId));

Filename filename = strFmt('export.csv');
System.IO.Stream stream = File::UseFileFromURL(downloadUrl);
File::SendFileToUser(stream, filename);
}
else
{
throw error("DMF execution failed and details were written to the execution log");
}
}
catch
{
error("error occurred while exporting");
}

venerdì 5 gennaio 2018

D365FFO: System.InvalidOperationException. Only properties of simple type can be key properties.

Actually there is a Know BUG if you use ODATA references from NUGET.


If a data entity has an enum type field on the entity primary key, the system will report this error:


An unhandled exception of type ' System.InvalidOperationException' occurred in Microsoft.OData.Client.dll
Additional information: The key property 'Type' on for type
'ProductsApp4.Microsoft.Dynamics.DataEntities.LegalEntityContact' is of
type 'System.Nullable`1[[ProductsApp4.Microsoft.Dynamics.DataEntities.LogisticsElectronicAddressMethodType, ProductsApp4, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]', which is not a simple type. Only properties of simple type can be key properties.


To resolve the issue, remove NUGET references from the project and use this custom OData dlls: in https://github.com/Microsoft/Dynamics-AX-Integration/tree/master/Packages



Pay attention if you are installing client on OS is Win7 x64 or Vista x64 and you got an error something like that:


{"Could not load file or assembly 'TheAssemblyYouHaveReferenced' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)":"TheAssemblyYouHaveReferenced'}



Refer to this article: https://blogs.msdn.microsoft.com/keithmg/2012/03/20/strong-name-validation-failed-exception-from-hresult-0x8013141a/

giovedì 4 gennaio 2018

D365FFO: The underlying connection was closed: An unexpected error occurred on a send when try to connect ODATA to UAT or PROD report error

As reported here https://community.dynamics.com/ax/f/33/t/227417

When you try to connect to UAT or PROD environement using ODATA the system will report this error: The underlying connection was closed: An unexpected error occurred on a send

Adding this line of code on connection

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

venerdì 22 dicembre 2017

D365FFO: Importing customers using data entity does not init value from customer group

Importing customers using dataentity does not init data from customer group, for example Payment Terms does not automatically inizialized from group.

This extension on CustCustomerEntity data entity will fix that problem

[ExtensionOf(dataentityviewstr(CustCustomerEntity))]
final class CustCustomerEntity_Extension
{
    [DataEventHandler(tableStr(CustCustomerEntity), DataEventType::MappedEntityToDataSource)]
    public static void CustCustomerEntity_onMappedEntityToDataSource(Common _sender, DataEventArgs _eventArgs)
    {
        CustCustomerEntity custCustomerEntity = _sender;
        DataEntityContextEventArgs dataEntityContextEventArgs = _eventArgs;

        DataEntityDataSourceRuntimeContext _dataSourceCtx = dataEntityContextEventArgs.parmEntityDataSourceContext();
        if (_dataSourceCtx.name() == dataEntityDataSourceStr(CustCustomerEntity, CustTable))
        {
            CustTable custTable = _dataSourceCtx.getBuffer();
            custTable.initFromCustGroup(CustGroup::find(custTable.CustGroup));
        }
    }
}

Apply to D365FFO Platform Update 11 Spring 2017

AX 2012: The request was aborted: Could not create SSL/TLS secure channel

The error you're encountering, "The request was aborted: Could not create SSL/TLS secure channel," can occur due to various re...