我正在尝试使用Azure AD oAuth 2身份验证访问Dynamics CRM Online REST API.为了做到这一点,我按照以下步骤操作:
- 我在Azure中注册了一个Web应用程序和/或web api
- 将动态CRM的权限配置为具有委派权限"以组织用户身份访问CRM Online"
- 并创建了一个密钥1年到期并保留客户ID.
在Azure上配置Web应用程序后,我在.NET/C#中创建了一个控制台应用程序,它使用ADAL发出简单请求,在这种情况下检索帐户列表:
class Program
{
private static string ApiBaseUrl = "https://xxxxx.api.crm4.dynamics.com/";
private static string ApiUrl = "https://xxxxx.api.crm4.dynamics.com/api/data/v8.1/";
private static string ClientId = "2a5dcdaf-2036-4391-a3e5-9d0852ffe3f2";
private static string AppKey = "symCaAYpYqhiMK2Gh+E1LUlfxbMy5X1sJ0/ugzM+ur0=";
static void Main(string[] args)
{
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(ApiUrl)).Result;
var clientCredential = new ClientCredential(ClientId, AppKey);
var authenticationContext = new AuthenticationContext(ap.Authority);
var authenticationResult = authenticationContext.AcquireToken(ApiBaseUrl, clientCredential);
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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 …Run Code Online (Sandbox Code Playgroud)