简体   繁体   English

从外部系统验证Dynamics CRM 2016 Rest Web服务

[英]Authenticate Dynamics CRM 2016 Rest Webservice from external system

I'm running into CORS issues when trying to call Dynamics WS from an external HTML page. 尝试从外部HTML页面调用Dynamics WS时遇到了CORS问题。

I looked at how to enable CORS on the server and did so by putting the following lines in the web.config of the CRM site. 我研究了如何在服务器上启用CORS ,并通过在CRM网站的web.config中添加了以下几行来实现。

<system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>

This seemed to solve the CORS issue but then raised a new error which stated: 这似乎解决了CORS问题,但随后引发了一个新错误,该错误指出:

Authentication failed 验证失败

I used the REST Builder tool to generate the javascript code. 我使用REST Builder工具生成javascript代码。 However, I'm not sure how to authenticate the call. 但是,我不确定如何验证呼叫。

The code I'm using is as follows: 我使用的代码如下:

var entity = {};
entity.new_name = "John Smith";
entity.new_guid = "KHU464KOU3";

var req = new XMLHttpRequest();
req.open("POST", "http://crmsitedomain.com/api/data/v8.2/new_consumer", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            alert("Created");
        } else {
            alert(this.status); // is returning "0"
        }
    }
};
req.send(JSON.stringify(entity));

I do see a "Token Header" and "Impersonate" options in the tool but where do I get the values for those parameters? 我在工具中确实看到了“令牌头”和“模拟”选项,但是我在哪里可以得到这些参数的值?

Firstly, I would undo those changes to the CRM site web.config , your not really meant to change the application files, it's probably unsupported, and is unlikely to fix your issue. 首先,我将撤消对CRM网站web.config更改,这并不是要更改应用程序文件,这实际上不受支持,并且不太可能解决您的问题。

The MSDN describes how to successfully authenticate Authenticate to Microsoft Dynamics 365 with the Web API . MSDN描述了如何使用Web API成功地对Microsoft Dynamics 365进行身份验证

Its a bit long winded to reproduce here so please check the links. 要在此处复制它有点漫长,所以请检查链接。

With Microsoft Dynamics 365 (online) or internet facing deployments When you use the Web API for Dynamics 365 (online) or an on-premises Internet-facing deployment (IFD) you must use OAuth as described in Connect to Microsoft Dynamics 365 web services using OAuth . 对于Microsoft Dynamics 365(在线)或面向Internet的部署,当您将Web API用于Dynamics 365(在线)或本地面向Internet的部署(IFD)时,必须按照使用以下方法连接到Microsoft Dynamics 365 Web服务中所述使用OAuth: OAuth

If you're creating a single page application (SPA) using JavaScript you can use the adal.js library as described in Use OAuth with Cross-Origin Resource Sharing to connect a Single Page Application to Microsoft Dynamics 365 . 如果要使用JavaScript创建单页应用程序(SPA),则可以使用adal.js库,如将OAuth与跨域资源共享配合使用中所述, 将单页应用程序连接到Microsoft Dynamics 365

Did you see this MSDN page? 您看到 MSDN页面了吗? This is for Web API, not WS, but its nearly the same 这是针对Web API而非WS的,但几乎相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM