web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Dynamics 365 V9.1 on premise integration with exchange online error 401 bad token

(6) ShareShare
ReportReport
Posted on by 51
We are to integrate Dynamics 365 V9.1 on premise with exchange online.
The procedure described by Microsoft page is followed step by step
- The Dynamics 365 hybrid connector is installed
- The app required to register the certificate is created on the tenant
- A valid certificate required for the S2S setup is installed in the CRM and deployed with the script provided on Github, the scrip is executed with success and the certificate deployment is checked
- The email server profile (Exchange online hybrid) is created.
 
The issue start with the connectivity test. CRM request an ACS token presented to EWS and the return is an error 401 because EWS doesn't expect this token issued by ACS.
EWS receive a request but the token presented is not accepted:
 
WWW-Authenticate: Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trusted_issuers="00000001-0000-0000-c000-000000000000@*", token_types="app_asserted_user_v1 service_asserted_app_v1", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize", error="invalid_token"
 
 
The procedure for the setup is follow step by step. We are blocked.
Does someone meet the same issue in the forum, all the good idea are welcome. 
We went in detail on the analysed tracing the traffic for the connection. The conclusion is ACS is not supported, or perhaps supported if some condition are met.
 
 
Categories:
I have the same question (0)
  • Suggested answer
    Daivat Vartak (v-9davar) Profile Picture
    7,835 Super User 2025 Season 2 on at
    Hello AP-19041205-0,
     

    You've pinpointed the core of the problem: Dynamics 365 v9.1 on-premises is attempting to use an ACS token for authentication against Exchange Online, which now expects OAuth 2.0 tokens. The WWW-Authenticate: Bearer header in the EWS response clearly indicates that it's expecting a modern authentication (OAuth 2.0) bearer token, not the older ACS (Access Control Service) token.

     

    Why is this happening and how to resolve it?

    The documentation you followed might be outdated or not specific enough for the current authentication requirements of Exchange Online. Modern Authentication (OAuth 2.0) is the standard and recommended authentication method for connecting to Exchange Online, and ACS has been deprecated.

     

    Here's a breakdown of why you're seeing this and the steps you need to take:

    The Issue: Outdated Authentication Flow

    Dynamics 365 v9.1 on-premises, by default, might be configured to use an older authentication flow that relies on ACS when integrating with Exchange Online. However, Exchange Online has moved towards modern authentication for enhanced security. This mismatch in expected token types is causing the 401 "invalid_token" error.

     

    Solution: Configure Dynamics 365 v9.1 for Hybrid Modern Authentication with Exchange Online

    You need to explicitly configure your Dynamics 365 v9.1 on-premises environment to use Hybrid Modern Authentication (OAuth 2.0) for its connection to Exchange Online. This involves several steps:

    1. Ensure Prerequisites for Hybrid Modern Authentication are Met:

     
    • Exchange Hybrid Configuration: You likely already have some form of Exchange Hybrid setup if you're trying to connect to Exchange Online. Ensure it's correctly configured and functioning.

    • Azure AD Connect: Azure AD Connect must be configured and synchronizing your on-premises Active Directory with Azure AD.

    • Exchange Online Configuration: Ensure Hybrid Modern Authentication is enabled in your Exchange Online tenant. You can check this using Exchange Online PowerShell:
      Get-OrganizationConfig | Format-List OAuth2ClientProfileEnabled
      It should return True. If not, you'll need to enable it.

    • Network Connectivity: Ensure your Dynamics 365 on-premises server can communicate with the necessary Microsoft Online Services endpoints.

    2. Register Dynamics 365 as an Application in Azure AD:

    While you might have created an app registration for the certificate, you might need a separate registration specifically for the OAuth 2.0 connection.

    • Go to the Azure Active Directory admin center.

    • Navigate to App registrations.

    • Click "New registration."

    • Give your application a name (e.g., "Dynamics 365 On-Premise - Exchange Online").

    • Select "Accounts in this organizational directory only (Single tenant)."

    • For Redirect URI, you can temporarily enter a placeholder like https://localhost. You might need to adjust this later depending on the specific authentication flow.

    • Click "Register."

    •  

    3. Configure API Permissions for the Dynamics 365 Azure AD Application:

    You need to grant this application permissions to access Exchange Online.

    • In your newly created Azure AD application, navigate to "API permissions."

    • Click "Add a permission."

    • Select "Microsoft Graph."

    • Choose "Application permissions."

    • Search for and select the following permissions (at a minimum):

      • Mail.Send

      • Mail.ReadWrite

      • Calendars.ReadWrite

      • User.Read (for user lookup) 

    • Click "Add permissions."

    • Grant admin consent for your organization for these newly added permissions.

    •  

    4. Configure Dynamics 365 v9.1 for OAuth:

    This is the crucial step that tells Dynamics 365 to use modern authentication. You'll typically need to modify the configdb of your Dynamics 365 organization. This should be done with caution and ideally in a test environment first.

    You'll need to use PowerShell commands to update the EmailServerProfile record to specify OAuth as the authentication type and provide the necessary Azure AD application details.

    Here's a general outline of the commands (you'll need to adapt the specific values):

    # Get the ID of your Exchange Online (Hybrid) Email Server Profile
    $emailServerProfile = Get-CrmRecord -conn $serverConfig -EntityName emailserverprofile -WhereAttribute "name" -WhereOperator Equal -WhereValue "Your Exchange Online (Hybrid) Profile Name"
    if ($emailServerProfile) {
        # Set the Authentication Type to OAuth
        Set-CrmRecord -conn $serverConfig -EntityName emailserverprofile -Id $emailServerProfile.emailserverprofileid -AttributeList @{"authenticationtype" = 0} # 0 for OAuth
        # Set the OAuth AppId (Client ID of the Azure AD Application)
        Set-CrmRecord -conn $serverConfig -EntityName emailserverprofile -Id $emailServerProfile.emailserverprofileid -AttributeList @{"oauthclientid" = "YOUR_AZURE_AD_APP_CLIENT_ID"}
        # Set the OAuth Secret (Client Secret of the Azure AD Application - if applicable, though often not needed for server-to-server)
        # Set-CrmRecord -conn $serverConfig -EntityName emailserverprofile -Id $emailServerProfile.emailserverprofileid -AttributeList @{"oauthsecret" = "YOUR_AZURE_AD_APP_CLIENT_SECRET"}
        # Set the OAuth Token Endpoint
        Set-CrmRecord -conn $serverConfig -EntityName emailserverprofile -Id $emailServerProfile.emailserverprofileid -AttributeList @{"oauthtokenendpoint" = "https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/token"}
        # Set the OAuth Authorization Endpoint
        Set-CrmRecord -conn $serverConfig -EntityName emailserverprofile -Id $emailServerProfile.emailserverprofileid -AttributeList @{"oauthauthorizationendpoint" = "https://login.microsoftonline.com/YOUR_TENANT_ID/oauth2/authorize"}
        Write-Host "Email Server Profile updated for OAuth."
    } else {
        Write-Error "Email Server Profile 'Your Exchange Online (Hybrid) Profile Name' not found."
    }

    Replace the placeholder values with your actual information:

    • Your Exchange Online (Hybrid) Profile Name: The exact name of your Email Server Profile in Dynamics 365.

    • YOUR_AZURE_AD_APP_CLIENT_ID: The Application (client) ID of the Azure AD application you registered in step 2.

    • YOUR_TENANT_ID: Your Microsoft 365 tenant ID (the Directory ID in Azure AD).

    •  

    Important Considerations for v9.1:

    • Update Rollups: Ensure your Dynamics 365 v9.1 on-premises environment has the latest applicable update rollups installed. Support for Hybrid Modern Authentication might have been introduced in a specific update. Check the Dynamics 365 v9.1 release notes for information on OAuth support for Exchange Online integration.

    • Hybrid Connector: The Dynamics 365 Hybrid Connector might play a role in facilitating this connection. Ensure it's correctly configured and running. Review its logs for any errors.

    • Certificate for S2S: The certificate you configured for S2S might still be necessary for other aspects of the hybrid connection, but the authentication against EWS will now rely on OAuth.

    • Testing: After making these changes, thoroughly test the connection and email sending/receiving from within Dynamics 365.

    •  

    Troubleshooting After Configuration:

    • Review Event Logs: Check the Event Viewer on your Dynamics 365 server for any errors related to email connectivity or authentication.

    • Trace CRM Logs: Enable tracing in Dynamics 365 to get more detailed logs of the connection attempts and any errors.

    • Azure AD Sign-in Logs: Monitor the sign-in logs for the Azure AD application you created to see if the authentication attempts are successful and if there are any errors reported there.

    •  

    In summary, the 401 "invalid_token" error indicates that Exchange Online is expecting a modern authentication token, while Dynamics 365 v9.1 is likely trying to use an older ACS token by default. You need to configure Dynamics 365 v9.1 to use Hybrid Modern Authentication (OAuth 2.0) by registering an application in Azure AD, granting it the necessary permissions, and updating the Email Server Profile in Dynamics 365 with the Azure AD application details and setting the authentication type to OAuth.

    Start by researching the specific steps and prerequisites for enabling Hybrid Modern Authentication for Exchange Online with Dynamics 365 v9.1 on-premises. The generic steps above should guide you, but consult the relevant Microsoft documentation for v9.1 for the most accurate and detailed instructions.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak
  • AP-19041205-0 Profile Picture
    51 on at
    Thanks a lot  for your detailed response
    The script I am referencing is provided by Microsoft on this page:
    If you look this page you will see a reference to a script on Github adapted, to deploy the S2S certificate on the tenant.
     
    My question what becomes the S2S authentication. If you look the script the certificate is associate to the global identifier corresponding to dataverse.
    Does this part remain in the process to allow Dynamics 365 on prem to be recognized by exchange or this process completly fall with the Modern authentication approach you propose.
    More crazy, it's now several time ACS is replaced, but how the official page are continuing to reference this process.
    What become the S2S certificate in this new approach, the same S2S beeing also used by exchange on prem we use today and we would do the same in the next step, using exchange online.
    The question that remain is the S2S certificate in the approach you propose and worst why in the official page, we have nothing presenting this new process.
    You will observe the scripts on Github was updated recently, and nobody think if they remain applicable.
     
    Your point of view about how remain the S2S authentication in this new process will be very nice to know.
    Regards
    Angelo
     
  • AP-19041205-0 Profile Picture
    51 on at
    As you mention , the script was generated , assisted by AI.
    I can't trust this script for several reason , the member referenced in the script don't exist in the emailserverprofile.
    you can verify this in the Microsoft page describing the entity fields
     
    The version we are using is V9.1 with the rollup update 9.1.17.29
    Looking all the release note coming after this version, there is no information in relation with the modern authorization supported by CRM to work with exchange online.
     
    I checked all the version coming after 9.1.17.29, there is nothing in the release note about the modern authorization with exchange support for CRM.
    Only the version 9.1.36.12 as a specific point for Sharepoint online integration :
     
    And as I said previously , we have no official page provided on Microsoft page presenting how Dynamics 365 V9.1 on prem can be integrated with exchange online.
     
    Regards
    Angelo
  • CW-11121959-0 Profile Picture
    51 on at
    Hello Angelo,
    Have you been able to connect your on-premise dynamics to exchange online? I am being asked to do the same but after reading the documentation I am still unclear on how to proceed.
    Thanks,
    Craig
  • AP-19041205-0 Profile Picture
    51 on at
    Hello Craig,
    No response for the moment. We are waiting a feedback from Microsoft.
    The documentation online is referencing ACS in the setup. But ACS is not supported for EWS.
    The release note related to the cumulative update say nothing on this subject.
     
    The documentation reference also a specific URL to use for US and it's also related to ACS.
     
     
    The cloud integration is using EWS with Dynamics 365 on premise.
    A recent communication from Microsoft says for non-Microsoft app , EWS requests will be blocked from October 2026.
     
     
    I asked to clarify what Microsoft call non-Microsoft app. Is Dynamics 365 on prem considered as supported or not?
    Angelo
     
  • CU21051753-0 Profile Picture
    13 on at
    I'm having the same issue on a new Office 365 tenant, but it works fine on an old one.
     
    The only difference I can see is that the old one has a Microsoft system app in Entra called 'CRMHybridConnector', but the new one doesn't (the hybrid connector has been 'purchased').
     
    I've opened a support ticket too.
  • CW-11121959-0 Profile Picture
    51 on at
    Any good news on this Angelo?
     
    I am in the same boat. ACS is disabled on Exchange Online so the MS documentation and scripts are useless.
     
    I can't find any reliable documentation on using OAuth 2.0 with client/secret.
     
    Thanks again,
    Craig
     
     
  • Verified answer
    AP-19041205-0 Profile Picture
    51 on at
    Hi Craig, for us the issue is fixed.
    In fact ACS in the tenant was responding to the request generated by the CRM.
    Just exchange was not accepting it.
    In fact discussing with Microsoft the Dynamics 365 Hybrid connector was reconfigured to allow exchange to accept ACS token.
    This traffic is monitored.
    The script provided was used to deploy the S2S certificate on the tenant.
    Microsoft for the new tenant don't activate ACS but for existing tenant configured to work in the past with the integration provided to
    support connection coming from Dynamics 365 On prem ACS is here but deactivated for the integration with exchange online.
    It will depend of the relationship you have with Microsoft and the importance of the pain created by this situation for your business.
    Regards
    Angelo
  • CW-11121959-0 Profile Picture
    51 on at
    Hi Angelo,
    Are you saying that after ACS was enabled in Exchange online the email integration started to work and that Microsoft had to enable it?
     
    According to MS Azure ACS will be fully retired as of April 2nd, 2026. (Azure ACS retirement in Microsoft 365 | Microsoft Learn)
     
    Thanks,
    Craig
     
  • AP-19041205-0 Profile Picture
    51 on at
    Hello Craig, I am saying ACS was active and when Dynamics 365 on prem contacted ACS we received the token.
    But the token was rejected by exchange online, because ACS was not configured with exchange online.
    Microsoft configured the tenant to allow ACS to work with exchange online.
    As you say there is the official communication provided on internet and there is the reality about the end of life of ACS.
    It depends on where each of us we are in the movement to the cloud, Microsoft knows that and configured the tenant.
    As I said we are in a case by case discussion with Microsoft.
    Regards
    Angelo
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 51 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans