3
votes

I'm trying to connect my api to dynamics 365 crm. When I get the token with postman I can access, but when I get it with ADAL , the request returns 401 unauthorized. If I hardcode the token got with postman on my app it works fine. I saw that postman returns me info that ADALnot (like refresh_token, token_id)

Get token with postman

Get token with ADAL

string resource = "https://******.crm2.dynamics.com/";
string clientId = "**************";
string clientSecret = "************"; 
string authority = "https://login.microsoftonline.com/*****/oauth2/authorize";

ClientCredential credential = new ClientCredential(clientId, clientSecret);
AuthenticationContext authContext = new AuthenticationContext(authority, true);
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, credential);
1
Did you ever get this resolved? I'm having the same issue and while I would like to blame The Dynamics team i suspect the real culprit is the Azure AD and the ADAL teamm12lrpv

1 Answers

0
votes

With postman you are acquiring token using OAuth 2.0 authorization code flow . Azure AD will return access token , refresh token ,id token upon a successful response.

With ADAL authContext.AcquireTokenAsync(resource, credential); function , you are acquiring token using client credentials flow . With this flow, the application presents its client credentials to the OAuth2 token issuing endpoint, and in return gets an access token that represents the application itself without any user information , so you will not get the id_token since no user information exists . Also ,there is no need for the application to get a refresh token. When the access token expires, it simply goes back to the OAuth2 token issuing endpoint to get a new one.

You could acquiring token using authorization code flow with ADAL , please click here for code sample .