Getting an error when trying to authenticate using REST API Dynamics CRM with Azure AD.

0 votes

I'm taking the following steps to access a Dynamics CRM Online REST API with Azure AD oAuth 2 Authentication:

- Registered a web app in Azure
- Configured the permissions to Dynamics CRM to have Delegated permissions "Access CRM Online as organization user"
- Created a Key with a 1 year expiration and kept the Client ID generated

After the web app was configured on Azure I created a Console app in .NET/C# that uses ADAL to make a simple request to retrieve a list of accounts: 
 

    class Program
{
    private static string ApiBaseUrl = "https://xxxxx.api.crm4.dynamics.com/";
    private static string ApiUrl = "https://xxxxx.api.crm4.dynamics.com/api/data/v8.1/";
    private static string ClientId = "2a5dcdaf-2036-4391-a3e5-9d0852ffe3f2";
    private static string AppKey = "symCaAYpYqhiMK2Gh+E1LUlfxbMy5X1sJ0/ugzM+ur0=";

    static void Main(string[] args)
    {
        AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(ApiUrl)).Result;

        var clientCredential = new ClientCredential(ClientId, AppKey);

        var authenticationContext = new AuthenticationContext(ap.Authority);
        var authenticationResult = authenticationContext.AcquireToken(ApiBaseUrl, clientCredential);

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);

        var result = httpClient.GetAsync(Path.Combine(ApiUrl, "accounts")).Result;         
    }
}

I can retrieve an access token successfully but when I try to do a httprequest to CRM I always get a 401 - Unauthorized status code. What am I missing?

Apr 25 in Azure by sabby
• 4,370 points
271 views

1 answer to this question.

0 votes

Dynamics 365 has started to support Server-to-Server (S2S) authentication. So, this code should work when you create an application user. For more information about how to make this authentication work check this website: https://msdn.microsoft.com/en-us/library/mt790170.aspx

answered Apr 25 by Prerna
• 1,940 points

Related Questions In Azure

0 votes
2 answers
+1 vote
2 answers

How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

Azure IoT Hub comes with 3 SDK's: Service SDK Device ...READ MORE

answered Apr 13, 2018 in Azure by null_void
• 3,220 points
645 views
0 votes
1 answer

How to add an App Settings to existing Azure Web Application using Azure Power Shell?

The example is for slot-specific settings, if ...READ MORE

answered Jun 13, 2018 in Azure by club_seesharp
• 3,450 points
230 views
0 votes
1 answer

I am getting a network related error saying "cannot connect to a server". need help here.

Actually i got the same error, it ...READ MORE

answered Apr 26, 2018 in Azure by null_void
• 3,220 points
90 views
0 votes
1 answer

Which cloud hosting is best ( AWS or Azure or Google)

Hello @Hammer, This totally depends on your needs, ...READ MORE

answered Aug 10, 2018 in AWS by Priyaj
• 57,300 points
77 views
+14 votes
23 answers

AWS vs Azure which one will have a more promising career?

Azure is integrating with a lot of ...READ MORE

answered Jul 4, 2018 in Career Counselling by Soumodip Sinha
2,075 views
0 votes
1 answer

Azure has 3 different types of storage accounts. Is there any major difference between them?

Azure Storage provides three types of storage ...READ MORE

answered Mar 4 in Azure by Prerna
• 1,940 points
723 views
0 votes
1 answer

How to check if multiple files exist in Azure container?

foreach loop is the most efficient way though. ...READ MORE

answered Mar 4 in Azure by Prerna
• 1,940 points
325 views
0 votes
1 answer
0 votes
1 answer