What is Web API?
Web API is implementation of OData (Open Data Protocol) which is an OASIS standard that defines the best practice for building for building and consuming RESTful APIs.
Web API Authentication
- Javascript Web Resources: You don't need to specify authentication pattern if call is made from the CRM application.
- CRM On-Premise: When you use the Web API for on-premises deployments you must include the user’s network credentials.
- CRM Online or IFD: When you use the Web API for CRM Online or an on-premises Internet-facing deployment (IFD) you must use OAuth.
Steps to register your IFD or CUSTOM CRM application in Azure Active Directory
Step 1: Login to Windows Azure portal
Step 2: Select your organization and click on applications
It will list down all the application configured in selected organization.
Step 3: To Add your CRM application click on Add button.
Step 4: Add an application my organization is developing
Step 5: Enter name of your application
*Note: Web application or WEB API is selected for web application and for desktop, console it should be native client application. Since our sample is native client application, hence selected “Native” option
Step 6: Click Next and add redirect URI
Step 7: Once application is added, we need to configure it
Step 8: Add Microsoft dynamics crm permission to this application
Step 9: Assign Delegate Permission to Microsoft Dynamics CRM
Step 10: Save the Changes
Step 11: Copy Client ID, that we will use in native client application
Console Application: This sample demonstration will use OAuth authentication to connect CRM Services and your application MyCRM registered in Azure active directory.
static void Main(string[] args)
{
// TODO Substitute your correct CRM root service address,
string resource = "https://XXXX.api.crm.dynamics.com/";
// TODO Substitute your app registration values that can be obtained after you
// register the app in Active Directory on the Microsoft Azure portal.
string clientId = "db329e3d-4d6d-4ef2-9195-bfad1daed689";
string redirectUrl = "https://birlasoft22.api.crm.dynamics.com/";
// Authenticate the registered application with Azure Active Directory.
AuthenticationContext authContext =
new AuthenticationContext("https://login.windows.net/common", false);
AuthenticationResult result = authContext.AcquireToken(resource, clientId, new
Uri(redirectUrl));
RunAsync(result).Wait();
}
static async Task RunAsync(AuthenticationResult result)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://XXXX.api.crm.dynamics.com/");
client.Timeout = new TimeSpan(0, 2, 0); // 2 minutes
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = await client.GetAsync("api/data/v8.0/accounts");
if (response.IsSuccessStatusCode)
{
string s = await response.Content.ReadAsStringAsync();
Console.Write(s);
}
Console.ReadKey();
}
}
Make sure to add nuGet package “Active Directory Authentication Library” in your project.
Output: Since we have not passed credentials in code, login window opens, here enter your credentials.
Once you sign-in JSON response will be returned.
Helpful Links:
https://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-store-dotnet-adal-sso-authentication/
https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/
https://msdn.microsoft.com/en-us/library/gg327838.aspx
https://msdn.microsoft.com/en-us/library/mt595798.aspx
https://msdn.microsoft.com/en-us/library/mt595799.aspx