Wednesday, July 13, 2022

Generate token from key vault parameters using x++ in D365FO.

 Generate token from key vault parameters using x++ in D365FO.

  •  We are saving our token URL in Key vault for security. By using the below code we will get the URL form Key Vault. 
  • We have used same client ID and client secret for key vault also. So I have configured that in key vault parameters. 

    
    str                           url, token;
    KeyVaultParameters            vaultParameter;
    KeyVaultCertificateTable      vaultCertificate;

    select vaultParameter where vaultParameter.Name == 'Name';// which we have created in Key vault.

    if (vaultParameter)
    {
        select vaultCertificate where vaultCertificate.KeyVaultRef == vaultParameter.RecId;

        KeyVaultClientHelper::removeCachedKeyVaultClient(vaultCertificate.KeyVaultCertificate);

        url = KeyVaultCertificateHelper::getManualSecretValue(vaultCertificate.RecId);
        // Here KeyVault will generate the Token URL.
AuthenticationResult authResult; ClientCredential clientCrendential = new ClientCredential(                     vaultParameter.AppClientId, vaultParameter.getClientSecretKey());                     // Based on client Id and client secret key we have created Key vault. AuthenticationContext authContext = new AuthenticationContext(url); authResult = authContext.AcquireToken('Scope or Resource(URL)', clientCrendential);         token = authResult.AccessToken; Info(token); }



Keep Daxing!!

No comments:

Post a Comment