Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does AcquireToken with ClientCredential fail with invalid_client (ACS50012)?

Why won't my Azure AD application allow an oauth client_credentials grant?

I want to use the Azure Graph API, but first I need an oauth token. To get the token, I am trying to use Microsoft.IdentityModel.Clients.ActiveDirectory aka ADAL version 1.0.3 (from NuGet).

I'm using the overload of AuthenticationContext.AcquireToken that takes a ClientCredential object. (I can't use the overload that prompts the user to login because I'm writing a service, not an app.)

I configured my Azure AD web application as described in various tutorials/samples (e.g. ADAL - Server to Server Authentication).

My code looks like:

AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/thommmondago.onmicrosoft.com");
ClientCredential cc = new ClientCredential("41151135-61b8-40f4-aff7-8627e9eaf853", clientSecretKey);
AuthenticationResult result = ac.AcquireToken("https://graph.windows.net", cc);

The AcquireToken line throws an exception:

sts_token_request_failed: Token request to security token service failed.  Check InnerException for more details

The inner exception is a WebException, and the response received looks like an oauth error:

{ "error":"invalid_client",
 "error_description":"ACS50012: Authentication failed."
 "error_codes":[50012],
 "timestamp":"2014-03-17 12:26:19Z",
 "trace_id":"a4ee6702-e07b-40f7-8248-589e49e96a8d",
 "correlation_id":"b304af2e-2748-4067-99d0-2d7e55b121cd" }

Bypassing ADAL and using curl with the oauth endpoint also gives the same error.

My code works if I use the details of the Azure application that I found here:

AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/graphDir1.onmicrosoft.com");
ClientCredential cc = new ClientCredential("b3b1fc59-84b8-4400-a715-ea8a7e40f4fe", "FStnXT1QON84B5o38aEmFdlNhEnYtzJ91Gg/JH/Jxiw=");
AuthenticationResult result = ac.AcquireToken("https://graph.windows.net", cc);

So it's not an error with my code. I think it's either an error with my Azure AD, or I've got the ClientCredential parameters wrong.

like image 858
tjleigh Avatar asked Mar 17 '14 14:03

tjleigh


3 Answers

This turned out to be an error in Windows Azure, there was nothing wrong with my code or config.

After Microsoft fixed the problem in Azure, I had to create a new application and it started working.

Forum answer from Microsoft:

Hi,

We are seeing some errors with applications created in a several day time range, ending yesterday. We are continuing to fix up these applications but I don't have a good eta when this will be done. I'm apologize for the impact here.

Can you try creating a new application and retying the operation with the new client id?

thanks

like image 59
tjleigh Avatar answered Nov 05 '22 06:11

tjleigh


Have a look at this link: https://azure.microsoft.com/en-gb/documentation/articles/resource-manager-net-sdk/

The latest version of Active Directory Authentication Library does not support AcquireToken method, instead you have to use AcquireTokenAsync method.

var result = await authenticationContext.AcquireTokenAsync(resource: "https://{domain}.onmicrosoft.com/{site-if applicable}", clientCredential: credential);
like image 2
Arun Kumar Avatar answered Nov 05 '22 05:11

Arun Kumar


I was having the same issue but only running the code directly from Azure (inside an Azure Website).

I solved upgrading 'Microsoft.IdentityModel.Clients.ActiveDirectory' package to '2.6.1-alpha'

like image 1
emmekappa Avatar answered Nov 05 '22 04:11

emmekappa