Share via


Authenticate to Microsoft Dynamics 365 with the Web API

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

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

Web API authentication patterns

There are three different ways to manage authentication when using the Web API.

With JavaScript in web resources

When you use the Web API with JavaScript within HTML web resources, form scripts, or ribbon commands you don’t need to include any code for authentication. In each of these cases the user is already authenticated by the application and authentication is managed by the application.

With on-premises 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 Microsoft Dynamics 365 (online) or internet facing deployments

When you use the Web API for Dynamics 365 (online) or an on-premises Internet-facing deployment (IFD) you must use OAuth as described in Connect to Microsoft Dynamics 365 web services using OAuth.

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 Microsoft Dynamics 365.

See Also

Use the Microsoft Dynamics 365 Web API
Web API types and operations
Perform operations using the Web API
Connect to Microsoft Dynamics 365 web services using OAuth
Use OAuth with Cross-Origin Resource Sharing to connect a Single Page Application to Microsoft Dynamics 365

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright