尝试使用Web API Dynamics 365 CRM-403禁止的错误

时间:2020-11-06 21:29:04

标签: c# azure-active-directory dynamics-crm microsoft-dynamics webapi

我正尝试遵循一些MS文档,以使用Web API与Dynamics 365 CRM集成,但由于组织使用不易连接的多因素身份验证方法,我陷入了困境。

我已经尝试在代码中使用应用密码,但失败。 已经在Azure AD上注册了该应用程序,已授予权限,已修改清单,并已生成密钥。

我最后一次基于MS文档所做的尝试是

    `using Microsoft.IdentityModel.Clients.ActiveDirectory;
    using System.Net.Http.Headers;
    using System.Net.Http;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Configuration;
    using Newtonsoft.Json;
    using static System.Console;

    namespace CRM_WebApi
    {
    class Program
    {
        static void Main(string[] args)
        {
            string serviceUrl = "https://MY-ORG.api.crm.dynamics.com";
            string clientId = "f8dea8ad-b993-4161-8743-***********X";
            string secret = "X*x*x*x*x*_lm2_DUo.0Dj_5_Wvkgu~eAY4";
            string redirectUrl = "http://localhost";

            AuthenticationContext authContext =
            new AuthenticationContext("https://login.microsoftonline.com/MY-ORG/oauth2/authorize");
            ClientCredential credential = new ClientCredential(clientId, secret);
            AuthenticationResult result = authContext.AcquireToken(serviceUrl, credential);
            //The access token
            string accessToken = result.AccessToken;

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(serviceUrl);
                client.Timeout = new TimeSpan(0, 2, 0);  //2 minutes  
                client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                client.DefaultRequestHeaders.Add("OData-Version", "4.0");
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                HttpRequestMessage request =
                    new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.1/WhoAmI");
                //Set the access token
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                HttpResponseMessage response = client.SendAsync(request).Result;
                if (response.IsSuccessStatusCode)
                {
                    //Get the response content and parse it.  
                    JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);
                    Guid userId = (Guid)body["UserId"];
                    Console.WriteLine("Your system user ID is: {0}", userId);
                }

              }
            }
        }
    }`

这次我获得了令牌,但是得到了403-Forbidden的响应。

{状态代码:403,原因短语:“禁止使用”,版本:1.1,内容:System.Net.Http.StreamContent, 标头:{x-ms-service-request-id:51067dc4-670c-4417-84b6-600044745e18
x-ms-service-request-id:04dd42b7-898d-4935-9c7e-20c1e7028a10
严格的运输安全性:max-age = 31536000; includeSubDomains REQ_ID:04dd42b7-898d-4935-9c7e-20c1e7025b20
AuthActivityId:fb3e62d9-4e1e-405f-846a-711e6ccc5555
X来源:18921112101886374119914120471128195219221222118018411711623541331812331911702441x x x * X来源:2441011722446104156421301162318117713035251169236256193231163106232101179236129x x x
公开时间:OPTIONS,GET,HEAD,POST时间允许来源:*日期:2020年11月6日星期五,格林尼治标准时间
Set-Cookie:ARRAffinity = f439e98480c5c889aa462a387e36ac04f192110c01737e1e00da32e45cedx x ; domain = MY-ORG.api.crm.dynamics.com;路径= /; 安全; HttpOnly内容长度:89允许:选项允许:GET允许:HEAD允许:POST}}

有人遇到过类似的问题吗?您能指导我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

根据您的代码,您引用此Connect as an app来获取访问令牌。

请注意,文档指出:

注册应用程序时,您需要执行许多与以下步骤相同的步骤: 演练:使用Azure Active Directory注册应用 遵循例外

您不需要授予 Access Dynamics 365作为组织用户 许可。

此应用程序将绑定到特定的用户帐户。

因此,您无需在Azure AD应用程序中添加委派权限。您需要做的是Common Data Service user account bound to the registered appManually create a Common Data Service application user

之后,您将获得具有足够权限的访问令牌。