【发布时间】:2016-08-27 17:46:47
【问题描述】:
我正在尝试使用 Dynamics CRM 2016 Online 和 Azure Active Directory 进行身份验证。我能够按照这里的所有步骤进行操作:
https://msdn.microsoft.com/en-us/library/mt622431.aspx 和 https://msdn.microsoft.com/en-us/library/gg327838.aspx
但这些步骤演示了如何设置用户名身份验证流程。我想使用客户端凭据流。我在 Azure AD 中创建了一个新应用 - 一个 Web 应用程序。我有一个客户端 ID 和一个应用程序密钥,并设置了 Dynamics CRM Online 的权限。我能够获取访问令牌,但在随后的调用中我收到此错误:
HTTP 错误 401 - 未经授权:访问被拒绝
我错过了一个步骤吗?有人知道某处的帖子提供了有关如何使此流程正常工作的详细信息吗?
这是我的代码:
string clientId = "<client id>";
string appKey = "<app key>";
// Get the authority and resource URL at runtime
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri("https://<org address>/api/data/")).Result;
String authorityUrl = ap.Authority;
String resourceUrl = ap.Resource;
// Authenticate the registered application with Azure Active Directory.
AuthenticationContext authContext = new AuthenticationContext(authorityUrl);
ClientCredential clientCredential = new ClientCredential(clientId, appKey);
AuthenticationResult result = authContext.AcquireToken(resourceUrl, clientCredential);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
HttpResponseMessage response = client.GetAsync("https://<org address>/api/data/v8.1/EntityDefinitions").Result;
【问题讨论】:
-
类似于stackoverflow.com/questions/37215742/… 他们的 API 可能不支持应用程序凭据。可能他们正在寻找凭据流获取的访问令牌中不存在的特定权限(由委派访问授予)。
-
请看我的类似问题的解决方案stackoverflow.com/questions/37454539/…
标签: azure oauth-2.0 dynamics-crm dynamics-crm-2016