消费的Azure托管的WebAPI在MS Dynamics CRM Online中 [英] Consuming Azure hosted WebApi in MS Dynamics CRM Online

查看:228
本文介绍了消费的Azure托管的WebAPI在MS Dynamics CRM Online中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用MS Dynamics CRM中2016年在网上,我们都需要消耗的Azure托管的WebAPI从客户端。我们正在试图让使用Ajax调用..同一code为我们工作MS动态CRM之外,但动态CRM中我们得到拒绝访问错误的数据。我们已经启用的的WebAPI CORS,但我们仍然这样的遭遇问题。它看起来像它关系到Dynamics CRM中的东西,但我们无法找到原因和解决方案。

下面是示例code这 MS工作动态CRM

之外

  $阿贾克斯({
            网址:'http://myaccountapi.azurewebsites.net/api/Account',
            输入:POST,
            数据:'testaccount',
            的contentType:应用/ JSON
            成功:功能(数据){
                过程数据(数据);            },
            错误:功能(错误){
                警报(error.statusText);
            }
        });

但是,这同样code会抛出错误的 CRM它说:访问被拒绝


解决方案

所以,你必须从 orgName.crmX.dynamics.com 页面试图打电话 myaccountapi.azurewebsites.net 。这不是一个CRM问题

您正在很可能导致拒绝访问的消息的跨站请求 - 因为,我猜,你有没有启用CORS中的WebAPI应用

您可以查看在完整的例子:的http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api.


  

启用CORS


  
  

现在,让我们能够在web服务应用CORS。首先,添加CORS的NuGet
  包。在Visual Studio中,从工具菜单,选择库包
  经理,然后选择包管理器控制台。在包管理器
  控制台窗口中,键入以下命令:


  
  

安装封装Microsoft.AspNet.WebApi.Cors


  
  

此命令安装最新的软件包,并更新所有依赖,
  包括核心Web API库。用户-Version标志目标
  特定版本。该CORS包需要的Web API 2.0或更高版本。


  
  

打开文件App_Start / WebApiConfig.cs。以下code加入到
  WebApiConfig.Register方法​​。

 使用System.Web.Http;
WebService的命名空间
{
    公共静态类WebApiConfig
    {
        公共静态无效的注册(HttpConfiguration配置)
        {
            //新的code
            config.EnableCors();            config.Routes.MapHttpRoute(
                名称:DefaultApi
                routeTemplate:API / {}控制器/(编号),
                默认:新{ID = RouteParameter.Optional}
            );
        }
    }
}


  
  

接下来,[EnableCors]属性添加到类的TestController:

 使用System.Net.Http;
使用System.Web.Http;
使用System.Web.Http.Cors;命名空间WebService.Controllers
{
    [EnableCors(来源:http://mywebclient.azurewebsites.net,标题:*,方法:*)]
    公共类的TestController:ApiController
    {
        //控制器方法没有显示...
    }
}


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 to myaccountapi.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.

Enable CORS

Now let's enable CORS in the WebService app. First, add the CORS NuGet package. In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command:

Install-Package Microsoft.AspNet.WebApi.Cors

This command installs the latest package and updates all dependencies, including the core Web API libraries. User the -Version flag to target a specific version. The CORS package requires Web API 2.0 or later.

Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.

using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // New code
            config.EnableCors();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

Next, add the [EnableCors] attribute to the TestController class:

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebService.Controllers
{
    [EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
}

这篇关于消费的Azure托管的WebAPI在MS Dynamics CRM Online中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆