Authenticate to Dynamics 365 Customer Engagement with the Web API

Note

The authentication documentation that is applicable to Dynamics 365 Customer Engagement app (online) users is now available in the Power Apps documentation at: Authenticate to Dataverse 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 Power Apps Dataverse topic: Authenticate to Dataverse 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 with Dataverse.

If you're creating a single page application (SPA) using JavaScript you can use the msal.js library as described in Use OAuth with Cross-Origin Resource Sharing to connect a Single Page Application.

See also

Use Dynamics 365 Customer Engagement Web API