是否可以使用服务主体在线对动态 crm 进行身份验证?

Is it possible to use a service principal to authenticate against dynamics crm online?

我尝试了以下方法:

            AuthenticationParameters ap = await AuthenticationParameters.CreateFromResourceUrlAsync(
                        new Uri($"{CrmServiceUrl}/api/data/"));

            var _authenticationContext = new AuthenticationContext(ap.Authority);

            AuthenticationResult result = await _authenticationContext.AcquireTokenAsync(ap.Resource, new ClientCredential(_clientID,secret));

            var client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

            var req = await client.GetAsync($"{CrmServiceUrl}/api/data/v8.1/accounts?$select=name&$top=3");
            Console.WriteLine(await req.Content.ReadAsStringAsync());

但正在

HTTP Error 401 - Unauthorized: Access is denied

clientid 和 secret 是 azure 广告服务主体。

我自己没有这样做,但我猜可能不会。要访问 CRM 中的数据,您需要在 CRM 中拥有一个用户帐户 - 这就是 CRM 安全模型在身份验证后应用的方式。

我建议您查看此处 Authenticate users in Microsoft Dynamics CRM,其中介绍了您可以使用 CRM 进行身份验证的所有方式。