Using OAuth Authentication
OAuth requires the authenticating user to interact with Dynamics 365 for Finance and Operations using the browser. The provider facilitates this in various ways as described below.
Embedded Credentials
Desktop Applications
See Embedded Credentials to connect with the provider's embedded credentials and skip creating a custom OAuth app.Headless Machines
See Headless Machines to skip creating a custom OAuth app and authenticate an application running on a headless server or another machine where the provider is not authorized to open a browser.Custom Credentials
Instead of connecting with the provider's embedded credentials, you can register an app to obtain the OAuthClientId and OAuthClientSecret.
When to Create a Custom OAuth App
Web Applications
You need to create a custom OAuth app in the web flow.
Desktop Applications
Creating a custom OAuth app is optional as the provider is already registered with Dynamics 365 for Finance and Operations and you can connect with its embedded credentials. You might want to create a custom OAuth app to change the information displayed when users log into the Dynamics 365 for Finance and Operations OAuth endpoint to grant permissions to the provider.
Headless Machines
Creating a custom OAuth app is optional to authenticate a headless machine; the provider is already registered with Dynamics 365 for Finance and Operations and you can connect with its embedded credentials. In the headless OAuth flow, users need to authenticate via a browser on another machine. You might want to create a custom OAuth app to change the information displayed when users log into the Dynamics 365 for Finance and Operations OAuth endpoint to grant permissions to the provider.
Creating a Custom OAuth App
See Creating a Custom OAuth App for a procedure.
Embedded Credentials
Authenticate using the Embedded OAuth Credentials
Desktop Authentication with the Embedded OAuth App
You can connect without setting any connection properties for your user credentials. Below are the minimum connection properties required to connect.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
When you connect the provider opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The provider then completes the OAuth process.
- Extracts the access token from the callback URL and authenticates requests.
- Obtains a new access token when the old one expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Custom Credentials
When to Use a Custom OAuth App
Creating a custom OAuth app is required in the web flow. Creating a custom OAuth app is optional for desktop and headless applications; the provider is already registered with Dynamics 365 for Finance and Operations and you can connect with its embedded credentials.You might want to create a custom OAuth app to change the information displayed when users log into Dynamics 365 for Finance and Operations to grant permissions to the provider.
Desktop Authentication with a Custom OAuth App
Follow the steps below to authenticate with the credentials for a custom OAuth app. See Creating a Custom OAuth App.Get and Refresh the OAuth Access Token
After setting the following, you are ready to connect:
- OAuthClientId: Set this to the client Id assigned when you registered your app.
- OAuthClientSecret: Set this to the client secret assigned when you registered your app.
- CallbackURL: Set this to the redirect URI defined when you registered your app.
- InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
When you connect the provider opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The provider then completes the OAuth process:
- Extracts the access token from the callback URL and authenticates requests.
- Refreshes the access token when it expires.
- Saves OAuth values in OAuthSettingsLocation to be persisted across connections.
Web Authentication with a Custom OAuth App
When connecting via a Web application, you need to register a custom OAuth app with Dynamics 365 for Finance and Operations. See Creating a Custom OAuth App. You can then use the provider to get and manage the OAuth token values.
Get an OAuth Access Token
Set the following connection properties to obtain the OAuthAccessToken:
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
You can then call stored procedures to complete the OAuth exchange:
-
Call the GetOAuthAuthorizationUrl stored procedure. Set the AuthMode input to WEB and set the CallbackURL input to the Redirect URI you specified in your app settings.
The stored procedure returns the URL to the OAuth endpoint.
- Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
- Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL.
Headless Machines
Using OAuth on a Headless Machine
To create Dynamics 365 for Finance and Operations data sources on headless servers or other machines on which the provider cannot open a browser, you need to authenticate from another machine. Authentication is a two-step process.
- Instead of installing the provider on another machine, you can follow the steps below to obtain the OAuthVerifier value. Or, you can install the provider on another machine and transfer the OAuth authentication values, after you authenticate through the usual browser-based flow.
- You can then configure the provider to automatically refresh the access token from the headless machine.
Using the Embedded OAuth Credentials
Obtain a Verifier Code
To obtain the OAuthVerifier value, log in at your organization's Dynamics 365 for Finance and Operations OAuth authorization URL and grant access permissions to the provider.
To obtain the authorization URL, replace the value of the resource parameter in the following URL with your OrganizationUrl.
https://login.microsoftonline.com/common/oauth2/authorize?resource=https%3A%2F%2Fmy-organization.crm.dynamics.crm&client_id=1d158b67-a602-4883-98e3-de18dd3ad133&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A33333 |
On the headless machine, set the following connection properties to obtain the OAuth authentication values.
- OAuthVerifier: Set this to the verifier code.
- InitiateOAuth: Set this to REFRESH.
- OAuthSettingsLocation: Set this to persist the encrypted OAuth authentication values to the specified file.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
After the OAuth settings file is generated, set the following properties to connect to data:
- OAuthSettingsLocation: Set this to the file containing the encrypted OAuth authentication values. Make sure this file gives read and write permissions to the provider to enable the automatic refreshing of the access token.
- InitiateOAuth: Set this to REFRESH.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
Transfer OAuth Settings
Follow the steps below to install the provider on another machine, authenticate, and then transfer the resulting OAuth values.
On a second machine, install the provider and connect with the following properties set:
- OAuthSettingsLocation: Set this to a writable text file.
- InitiateOAuth: Set this to GETANDREFRESH.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
On the headless machine, set the following connection properties to connect to data:
- InitiateOAuth: Set this to REFRESH.
- OAuthSettingsLocation: Set this to the path to your OAuth settings file. Make sure this file gives read and write permissions to the provider to enable the automatic refreshing of the access token.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
Using the Credentials for a Custom OAuth App
Create a Custom OAuth App
Creating a custom OAuth app is optional in the headless OAuth flow; you can skip creating an app by connecting with the provider's embedded OAuth credentials. You might want to create a custom OAuth app to change the information displayed when users log into Dynamics 365 for Finance and Operations to grant permissions to the provider.
See Creating a Custom OAuth App for a procedure. You can then follow the procedures below to authenticate and connect to data.
Obtain a Verifier Code
Set the following properties on the headless machine:
- InitiateOAuth: Set this to OFF.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
You can then follow the steps below to authenticate from another machine and obtain the OAuthVerifier connection property.
- Call the GetOAuthAuthorizationUrl stored procedure with the CallbackURL input parameter set to the exact Redirect URI you specified in your app settings.
- Open the returned URL in a browser. Log in and grant permissions to the provider. You are then redirected to the callback URL, which contains the verifier code.
- Save the value of the verifier code. You will set this in the OAuthVerifier connection property.
On the headless machine, set the following connection properties to obtain the OAuth authentication values:
- OAuthVerifier: Set this to the verifier code.
- OAuthSettingsLocation: Set this to persist the encrypted OAuth authentication values to the specified file.
- InitiateOAuth: Set this to REFRESH.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
Connect to Data
After the OAuth settings file is generated, set the following properties to connect to data:
- OAuthSettingsLocation: Set this to the file containing the encrypted OAuth authentication values. Make sure this file gives read and write permissions to the provider to enable the automatic refreshing of the access token.
- InitiateOAuth: Set this to REFRESH.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
Transfer OAuth Settings
Follow the steps below to install the provider on another machine, authenticate, and then transfer the resulting OAuth values.
On a second machine, install the provider and connect with the following properties set:
- OAuthSettingsLocation: Set this to a writable text file.
- InitiateOAuth: Set this to GETANDREFRESH.
- OAuthClientId: Set this to the client Id assigned when you registered your app.
- OAuthClientSecret: Set this to the client secret assigned when you registered your app.
- CallbackURL: Set this to the Redirect URI you specified in your app settings.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
Test the connection to authenticate. The resulting authentication values are written, encrypted, to the path specified by OAuthSettingsLocation. Once you have successfully tested the connection, copy the OAuth settings file to your headless machine. On the headless machine, set the following connection properties to connect to data:
- InitiateOAuth: Set this to REFRESH.
- OAuthSettingsLocation: Set this to the path to your OAuth settings file. Make sure this file gives read and write permissions to the provider to enable the automatic refreshing of the access token.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
Refresh the OAuth Access Token
You can set InitiateOAuth to REFRESH to automatically refresh the OAuth access token when it expires, or you can call the RefreshOAuthAccessToken stored procedure to refresh the token manually.
Automatic Refresh
To refresh the token with InitiateOAuth, set the following on the first data connection:
- OAuthAccessToken: Set this to the access token returned by GetOAuthAccessToken.
- InitiateOAuth: Set this to REFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
- OAuthSettingsLocation: Set this to the path where the provider will save the OAuth values, to be persisted across connections.
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- OAuthRefreshToken: Set this to the refresh token returned by GetOAuthAccessToken.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
On subsequent data connections, set the following:
- InitiateOAuth
- OAuthSettingsLocation
- OrganizationUrl
- Tenant (optional)
- CrossCompany
Manual Refresh
You can use the RefreshOAuthAccessToken stored procedure to manually refresh the OAuthAccessToken. Call the stored procedure after the ExpiresIn parameter value returned by GetOAuthAccessToken has elapsed. You need the following connection properties to be set:
- OAuthClientId: Set this to the Client Id in your app settings.
- OAuthClientSecret: Set this to the Client Secret in your app settings.
- OrganizationUrl: Set this to the organization URL you are connecting to, such as https://[organization].crm.dynamics.com.
- Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.
You can then call RefreshOAuthAccessToken with the following parameter set:
- OAuthRefreshToken: Set this to the OAuthRefreshToken returned by GetOAuthAccessToken.
Creating a Custom OAuth App
When to Create a Custom OAuth App
Creating a custom OAuth app is required in the web flow. Creating a custom OAuth app is optional for desktop and headless applications; the provider is already registered with Dynamics 365 for Finance and Operations and you can connect with its embedded credentials.You might want to create a custom OAuth app to change the information displayed when users log into the Dynamics 365 for Finance and Operations OAuth endpoint to grant permissions to the provider.
Follow the steps below to create a custom OAuth app and obtain the connection properties in a specific OAuth authentication flow.
Steps to Create a Custom OAuth App
Follow the steps below to obtain the OAuth values for your app, the OAuthClientId and OAuthClientSecret.
- Log in to https://portal.azure.com.
- In the left-hand navigation pane, select Azure Active Directory then App Registrations and click the Add button.
- Select Web app / API.
- Enter an app name and any URL you would like for the sign-on URL. The sign-on URL is not used by the provider or in the authentication step, so it can be set to your home page or an arbitrary URL like http://localhost.
- After creating the app, in the Properties section of your app settings, set the Multi-Tenanted option to Yes.
- In the Reply URLs, enter http://localhost:33333, the provider's default. Or, set a different port of your choice and set CallbackURL to the exact reply URL you defined.
- In the Keys section, create a key for the app and select a duration.
- After you save the key, a value for the key is displayed once. Set OAuthClientSecret to the key value. Set OAuthClientId to the Application Id.
- Select Required Permissions and then click Add. Under Select an API, select the Dynamics 365 Finance and Operations permissions.
- Save your changes.
Client Credentials
Client credentials refers to a flow in OAuth where there is no direct user authentication taking place. Instead, credentials are created for just the app itself. All tasks taken by the app are done without a default user context. This makes the authentication flow a bit different from standard.
Client OAuth Flow
All permissions related to the client oauth flow require admin consent. This means the app embedded with the CData ADO.NET Provider for Dynamics 365 Finance and Operations 2019 cannot be used in the client oauth flow. You must create your own OAuth app in order to use client credentials. See Creating a Custom OAuth App for more details.
In your App Registration in portal.azure.com, navigate to API Permissions and select the Microsoft Graph permissions. There are two distinct sets of permissions - Delegated and Application permissions. The permissions used during client credential authentication are under Application Permissions. Select the applicable permissions you require for your integration.
In addition to setting the standard OAuth client id and secret, to use client credentials you must also set the following connection properties:
- OAuthGrantType: Set this to CLIENT.
- Tenant: Set this to the tenant you wish to connect to.
When you authenticate using client credentials, there is no Web flow as discussed under Using OAuth Authentication. Authentication with client credentials will take place automatically like any other connection, except there will be no window opened prompting the user. Because there is no user context, there is no need for a browser popup. Connections will take place and be handled internally.
Connecting from OnPremise
OnPremise connections are pretty much identical to Online connections with the following differences:
- Edition - Set this to OnPremise.
- ADFSServer - Set this to the ADFS Server being used to connect.
Creating an OAuth App
In order to connect to an OnPremise edition, you must create an OAuth app and supply the OAuthClientId and OAuthClientSecret. Please see the following guide on how to create an app in ADFS. After creating the app, map the following values during the OAuth app creation to these connection properties:
- OAuthClientId: Set this to the value of Client Identifier.
- CallbackURL: Set this to the value of the Redirect URI.
- OAuthClientSecret: Set this to the value of Secret after generating a shared secret.
- OAuthGrantType: Set this to CLIENT if you are setting up client credentials, as detailed here.
Advanced Settings
CData ADO.NET Provider for Dynamics 365 Finance and Operations 2019 - Build 19.0.7354
The provider enables granular control useful in more complex integrations or network topologies. In this section you can find more advanced provider configurations, as well as steps to connect from behind a firewall or troubleshoot connection errors.
Customizing the SSL Configuration
By default, the provider attempts to negotiate SSL/TLS by checking the server's certificate against the system's trusted certificate store. To specify another certificate, see the SSLServerCert property for the available formats to do so.
Connecting Through a Firewall or Proxy
HTTP Proxies
To connect through the Windows system proxy, you do not need to set any additional connection properties. To connect to other proxies, set ProxyAutoDetect to false.
In addition, to authenticate to an HTTP proxy, set ProxyAuthScheme, ProxyUser, and ProxyPassword, in addition to ProxyServer and ProxyPort.
Other Proxies
Set the following properties:
- To use a proxy-based firewall, set FirewallType, FirewallServer, and FirewallPort.
- To tunnel the connection, set FirewallType to TUNNEL.
- To authenticate, specify FirewallUser and FirewallPassword.
- To authenticate to a SOCKS proxy, additionally set FirewallType to SOCKS5.
Troubleshooting the Connection
To show provider activity from query execution to network traffic, use Logfile and Verbosity. The examples of common connection errors below show how to use these properties to get more context. Contact the support team for help tracing the source of an error or circumventing a performance issue.
- Authentication errors: Typically, recording a Logfile at Verbosity 4 is necessary to get full details on an authentication error.
- Queries time out: A server that takes too long to respond will exceed the provider's client-side timeout. Often, setting the Timeout property to a higher value will avoid a connection error. Another option is to disable the timeout by setting the property to 0. Setting Verbosity to 2 will show where the time is being spent.
- The certificate presented by the server cannot be validated: This error indicates that the provider cannot validate the server's certificate through the chain of trust. If you are using a self-signed certificate, there is only one certificate in the chain.
To resolve this error, you must verify yourself that the certificate can be trusted and specify to the provider that you trust the certificate. One way you can specify that you trust a certificate is to add the certificate to the trusted system store; another is to set SSLServerCert.