【问题标题】:Dynamics CRM - Unable to authenticate to Web API to consume data?Dynamics CRM - 无法对 Web API 进行身份验证以使用数据?
【发布时间】:2017-07-17 10:14:14
【问题描述】:

可能是重复的,但由于我没有找到确切的答案,所以我发布了这个。

我已获得 Dynamics CRM Web api 的凭据,我在我的代码中使用它们,如下所示:

string username = @"user.crm@tmeic.in";
    string password = @"XXXXXXXX";
    string domain = @"tmeictest.crm8.dynamics.com";
    string apiURL = @"https://tmeictest.api.crm8.dynamics.com/api/data/v8.2/";

然后,我使用如下方法初始化客户端:

HttpClient client = GetNewHttpClient(username, password, domain, apiURL);

public HttpClient GetNewHttpClient(string userName, string password, string domainName, string webAPIBaseAddress)
    {
        HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential(userName, password, domainName) });
        client.BaseAddress = new Uri(webAPIBaseAddress);
        client.Timeout = new TimeSpan(0, 2, 0);
        return client;
    }

我正在呼吁回应

HttpResponseMessage msg = client.GetAsync(apiURL).Result;

但它给了

未授权状态 401。

我直接在浏览器中进行了检查,并且能够登录。但是在我的代码中使用它时,它不会进行身份验证。

我这里有什么遗漏吗?

【问题讨论】:

    标签: dynamics-crm dynamics-crm-webapi


    【解决方案1】:

    问题中的代码仅适用于本地 CRM。我尚未测试以下解决方案,但您可以尝试一下。下面的客户 ID 将是您在向 AAD 注册 CRM 时收到的客户 ID。步骤here.

    var client = new HttpClient();
    
    var authenticationContext = new AuthenticationContext(
        authenticationParameters.Authority, false);
    
    AuthenticationParameters authenticationParameters =
        AuthenticationParameters.CreateFromResourceUrlAsync(
            "https://tmeictest.api.crm8.dynamics.com").Result;
    
    var userCredential = new UserCredential(@"user.crm@tmeic.in", @"XXXXXXXX");
    
    AuthenticationResult authenticationResult =
        authenticationContext.AcquireToken(
            authenticationParameters.Resource, @"" /* clientId */, userCredential);
    
    client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
    
    HttpResponseMessage msg = client.GetAsync(apiURL).Result;
    

    【讨论】:

      【解决方案2】:

      这是来自 MSDN article 的一个示例,其中包含大量 C# 的 WebAPI 示例代码:

        /// <summary>
        /// Obtains the connection information from the application's configuration file, then 
        /// uses this info to connect to the specified CRM service.
        /// </summary>
        /// <param name="args"> Command line arguments. The first specifies the name of the 
        ///  connection string setting. </param>
        private void ConnectToCRM(String[] cmdargs)
        {
         //Create a helper object to read app.config for service URL and application 
         // registration settings.
         Configuration config = null;
         if (cmdargs.Length > 0)
         { config = new FileConfiguration(cmdargs[0]); }
         else
         { config = new FileConfiguration(null); }
         //Create a helper object to authenticate the user with this connection info.
         Authentication auth = new Authentication(config);
         //Next use a HttpClient object to connect to specified CRM Web service.
         httpClient = new HttpClient(auth.ClientHandler, true);
         //Define the Web API base address, the max period of execute time, the 
         // default OData version, and the default response payload format.
         httpClient.BaseAddress = new Uri(config.ServiceUrl + "api/data/");
         httpClient.Timeout = new TimeSpan(0, 2, 0);
         httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
         httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
         httpClient.DefaultRequestHeaders.Accept.Add(
             new MediaTypeWithQualityHeaderValue("application/json"));
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-15
        • 1970-01-01
        • 2016-09-06
        • 1970-01-01
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多