PhuocLe - Microsoft Dynamics CRM

Dynamics 365 Online S2S Authentication - Full Explain Apr/02/2017

Server-to-Server (S2S) authentication (only support Dynamics 365 online) was great, it use ClientId/SecretKey to authentication (instead use UserName/Password) and it use without Dynamics 365 license.
But everybody have problem when use CllientId/SecretKey, the main problem is HTTP Error 401 - Unauthorized: Access is denied
I headache this problem more than 3 days, and now, I succeed authentication with ClientId/SecretKey.
This post I will fully explain you how I can achieve it.
Important noted: do the correct order step by step here
1. Register App with Azure Active Directory
After you register App, save these value here to use it later
a. Application ID
b. Url redirect
c. SecretKey
Save Application ID for use later.
Save Redirect URI for use later
Save SecretKey for use later. Rember the SecretKey only show once.
Note: When register App use:  
  • Application Type = Native and
  • Assign Permission to Dynamics CRM Online
2. Create new Office365 user
We call this user as ApplicationUser
a. Assign CRM Online license
b. Role: Dynamics 365 Service Administrator
Create new Office 365 user with Dynamics 365 license and role
3. Create a custom security role and assgin to ApplicationUser
Open Dynamics 365 by CRM Administrator account
a. Clone a System Administrator security role to new ApplicationUserRole
b. Assign ApplicationUserRole to ApplicationUser user
Clone System Administrator role to ApplicationUserRole
Assign ApplicationUserRole to ApplicationUser
c. Open Application User form for this ApplicationUser
i. Change form from User to Application User (if not change)
ii. Update field Application ID from step 1.a (Application ID)
iii. Save and go back to Enabled Users
Update Application ID for ApplicationUser
d. Change view to Application Users and check field Application ID URI for ApplicationUser empty
Make sure Application ID URI of ApplicationUser is emtpy
4. Remove Dyanmics 365 license
Back to 365 admin and remove license from ApplicationUser you already do in step 2
S2S Authentication no need a license, if you assign license to ApplicationUser it not work.
5. Manually build url and use it once
The syntax url here{talent-id}/oauth2/authorize?client_id={application-id}&response_type=code&redirect_uri={url-redirect}&response_mode=query&resource={crm-url}&state={new-guid}
  • talent-id: your azure active directory id. Azure portal -> Azure Active Directory -> Properties -> Directory ID
  • application-id: step 1.a
  • url-redirect: step 1.b
  • crm-ur: your full Dynamics 365 url, E.g.:
  • new-guid: generator a new GUID
My final manually url here:
6. Grant ApplicationUser to App
  • Use the manually url build on step 5. Open IE private window and paste the url to IE address bar
  • Process first time login (and only do it once) with the ApplicationUser you create in step 2, it redirect to url-redirect. Leave it and close your IE browser.
First and only once login to grant permission to ApplicationUser
7. Check Application ID URI
Goback to Application Users view and check the Application ID URI (step 3.e.1) now have value same value with Applcation ID.
  • The key I found here, if this field NULL, 401 return.
  • Only this way update this field (step 5)
Application ID URI updated
8. Build console application
a. Create a console application
b. Use code below and now you can see WhoAmI
Note: you can use authenticationResult.AccessToken for WebAPI call in the header request
class Program
    static private Uri GetServiceUrl(string organizationUrl)
        return new Uri(organizationUrl + @"/xrmservices/2011/organization.svc/web?SdkClientVersion=8.2");

    static void Main(string[] args)
        var organizationUrl = "";
        var aadInstance = "";
        var tenantID = "12b5c856-99c8-4268-b99f-b5bfd02ae0f3";
        var clientId = "2015d711-986e-4728-87d5-228994e190ba";
        var appKey = "your-private-secret-key-here";
        var clientcred = new ClientCredential(clientId, appKey);
        var authenticationContext = new AuthenticationContext(aadInstance + tenantID);
        var authenticationResult = authenticationContext.AcquireToken(organizationUrl, clientcred);
        var requestedToken = authenticationResult.AccessToken;
        using (var sdkService = new OrganizationWebProxyClient(GetServiceUrl(organizationUrl), false))
            sdkService.HeaderToken = requestedToken;
            var request = new OrganizationRequest()
                RequestName = "WhoAmI"
            var response = sdkService.Execute(new WhoAmIRequest()) as WhoAmIResponse;

Run the code and I get the result here

I get UserID GUID of ApplicationUser we already grant permission to App

Now in your 3rd app, you can access CRM by use ClientId/SecretKey (without use UserName/Password) and no need Dynamics 365 license. 

GREAT !!!!!!!
Some mistakes
  • Don’t assign Application ID to user.
  • Don’t create a custom security role and assign to application user, everybody always use default System Administrator role and assign to application user.
  • Build correct url above but login with another user that have assign Application ID, or login with user don’t assign application key
  • Forgot remove license from user before use url above.
  • Manually url not match with url-redirect when register application in Azure Active Directory
  • Don't do correct order I list here
Finally your target
  • Application User should in Application Users view
  • Application User should have 3 field (not blank)
    • Application ID
    • Application ID URI
    • Azure AD Object ID
  • Application User should remove Dynamics 365 license
  • Application User must have a custom security role

Hope this post working for you.

Category: CRM
wrote: May/16/2017

We followed your steps and it works! I cannot believe how complicated it is to push a lead from server to server with Dynamics. Thank you anyway for helping solve this.

Sander avatar Sander
wrote: Jun/14/2017

Wow.. so much blogs so much info but none explains it like you do. Took me a day to find your blog. Took me 5 minutes to actually be able to read the crm data through s2s clientid/secret combi. I got 401 all the way; for me the trick was the cloning of the admin role.. suddenly the url of the app user was updated with the client id and my 401 disappeared.

Thnx a million for posting this walktrhough!!


Srinivasa Rao Asapu avatar Srinivasa Rao Asapu
wrote: Jul/19/2017

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Bearer authorization_uri=, resource_id='

Srinivasa Rao Asapu avatar Srinivasa Rao Asapu
wrote: Jul/19/2017

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Bearer authorization_uri=, resource_id='

Srinivasa Rao Asapu avatar Srinivasa Rao Asapu
wrote: Jul/19/2017

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Bearer authorization_uri=, resource_id='

E. avatar E.
wrote: Sep/12/2017

Great post: it contains a lot of information that are missing from Microsoft's own documentation. Thanks a lot!

wrote: Sep/12/2017

Thanks for a useful post.

I was getting error Principal user (Id={guid...}, type=8) is missing prvReadAccount when trying to use this token.

I found that by enabling the licence again for that account it started working OK.

I was on the 1 month free trial and the licence type was 'Dynamics 365 Customer Engagement Plan Enterprise Edition'

Sander avatar Sander
wrote: Nov/23/2017

I am in the mist.. how can one get client secret from native app? I cannot see the key's menu option if I use native.. only if I state web api. I found this question stating native does not support client key Any ideas?

Emir avatar Emir
wrote: Nov/23/2017


I have problem to save application id on user form for my app user?

One question regarding your artikel: How you create key for Nativ applikation? Key is posible only for Web/APi application.

Regarding Emir

wrote: Dec/18/2017

Absolute lad. If only MS could write half decent documentation.

genos avatar genos
wrote: Jan/24/2018

Great post! I try 2 days to find to configure this. Very detailed explanation! Thanks!

wrote: Mar/22/2018


I am unable to generate Application ID URI mentioned in Step 5.

My Manual URL -{new-guid}

When I run this URL in IE, I am getting below error.

This site can’t be reached

localhost refused to connect.

Search Google for localhost 3979 api OAuth Callback


Kindly help



Stu avatar Stu
wrote: Apr/20/2018


When I into CRM to add the Application ID, I do not have a Application User form nor the Application ID field (to add to the standard form manually). Any ideas?

D365 v9.01



wrote: Oct/17/2018

Hi, do you know how to get key for native registered app?


wrote: Oct/17/2018

Hi, do you know how to get key for native registered app?


Enter your comment

Your Name (*)
Your Email (*)
Website (*)


About Me