We are using MS Dynamics Crm 2016 Online, we are required to consume Azure Hosted WebApi from client side. We are trying to get data using ajax call.. the same code works for us outside MS Dynamics Crm but within Dynamics Crm we are getting Access denied error. We have enabled CORS in the webapi but we still experince this issue. It looks like it is something related to Dynamics CRM but we are not able to find the cause and solution.
Below is the sample code which works outside MS Dynamics CRM
$.ajax({
url: 'http://myaccountapi.azurewebsites.net/api/Account',
type: 'POST',
data: 'testaccount',
contentType: "application/json",
success: function(data) {
processData(data);
},
error: function (error) {
alert(error.statusText);
}
});
But this same code throws error within CRM which says : Access denied.
So you have a page from
orgName.crmX.dynamics.com
trying to call tomyaccountapi.azurewebsites.net
. This is not a CRM issue.You are making a cross-site request that is likely resulting in an access denied message - since, I'm guessing, you have not enabled CORS in the WebApi application.
You can review the full example at: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api.
As CRM Online is accessed on an encrypted connection you also need to access external resources using SSL. Just change your url to use https and you should be good to go: