Authenticate to Dynamics 365 for Customer Engagement with the Web API

Note

This topic contains information that is applicable only for the on-premises users of Customer Engagement apps.

The authentication documentation that is applicable to Dynamics 365 for Customer Engagement app (online) users is now available in the PowerApps documentation at: Authenticate to Common Data Service with the Web API.

The code you write to manage authentication when using the Web API depends on the type of deployment and where your code is.

Authenticate with JavaScript in web resources

For information on how to authenticate with JavaScript in web resources, see this PowerApps Common Data Service topic: Authenticate to Common Data Service with the Web API.

Web API authentication with On-premise deployments

When you use the Web API for on-premises deployments you must include the user’s network credentials. The following example is a C# function that will return an HttpClient configured for a given user’s network credentials:

private HttpClient getNewHttpClient(string userName,string password,string domainName, string webAPIBaseAddress)  
{  
    HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(userName, password, domainName) });  
    client.BaseAddress = new Uri(webAPIBaseAddress);  
    client.Timeout = new TimeSpan(0, 2, 0);  
    return client;  
}  

With Dynamics 365 for Customer Engagement or Internet-facing deployments

When you use the Web API for Customer Engagement or an on-premises Internet-facing deployment (IFD) you must use OAuth as described in Use OAuth to connect to Dynamics 365 for Customer Engagement for apps web Services.

If you’re creating a single page application (SPA) using JavaScript you can use the adal.js library as described in Use OAuth with Cross-Origin Resource Sharing to connect a Single Page Application to Dynamics 365 for Customer Engagement.

See also

Use Dynamics 365 for Customer Engagement Web API