Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

CORS error occurred during the token retrieval process from Power pages

Posted on by 680
Hi,

We have a requirement from our client for power pages. To fulfill the requirement we have created a custom action in Dynamics 365 CRM and tried to execute it from power pages (we have made as public website).

We attempted to perform CRM operations on a power page for an external website. To execute these operations, we need to obtain an authorization token. However, during the token acquisition process, we encountered a CORS error, as indicated below.

Access to XMLHttpRequest at 'https://login.microsoftonline.com/9dedbf2a-1967-42da-b7e3-24a5e2c16475/oauth2/token' from origin 'https://test-demo12345.powerappsportals.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

We are using the below code for the same.
 
var form = new FormData();
            form.append(/grant_type/, /client_credentials/);
            form.append(/client_id/, /Enter Client Id/);
            form.append(/client_secret/, / Enter Client Secret /);
            form.append(/resource/, /Enter CRM URL/);
           
            var settings = {
              /url/: https://login.microsoftonline.com/Enter Tenant Id/oauth2/token/,
              /method/: /GET/,
              /timeout/: 0,
              /headers/: {
                /Cookie/: /fpc=Avy3L6riZPxJsdXm9EXO0xI0P776AQAAALsnF90OAAAA; stsservicecookie=estsfd; x-ms-gateway-slice=estsfd/,
                /Content-Type/: /application/jsonp/,
                /Access-Control-Allow-Origin/: /*/,
                /Access-Control-Allow-Methods/: /POST,GET,PUT/,
                /Access-Control-Allow-Headers/: /Content-Type,X-Auth-Token,Origin,Authorization/,
              },
              /processData/: false,
              /mimeType/: /multipart/form-data/,
              /contentType/: false,
              /credentials/: /include/,
              /data/: form
            };
           
            $.ajax(settings).done(function (response) {
              alert(response);
            });
 
Note: It is working from postman.

Has anyone else encountered a similar issue and found a solution for it.
 
Thanks!
 
  • Suggested answer
    Likith Profile Picture
    Likith 4 on at
    CORS error occurred during the token retrieval process from Power pages
    Hello,

    It seems like you're encountering a CORS (Cross-Origin Resource Sharing) issue when attempting to execute CRM operations from a Power Page on an external website.

    Considering the information in Manage your site's Content Security Policy | Microsoft Learn, you can follow below steps to address the issue:
     
    • In the Portal Management app, under Site Settings, review the 'HTTP/Content-Security-Policy' settings. Make sure the 'script-src' directive includes the domains you're making requests to. Adjust as needed to allow communication with 'login.microsoftonline.com'.
    • HTTP/Access-Control-Allow-Origin: User URL of the Microsoft Dataverse instance, such as https://contoso.crm.dynamics.com. To allow any URI to access your resources, use *. 
      This setting controls which domains are allowed to make requests to your Power Pages site. If you want to allow any URI to access your resources, you can set the value to '*'. (
      Set up HTTP headers in Power Pages | Microsoft Learn).

    ​​​​​​​Best regards,
    Likith Khareedu

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans