Dynamics CRM - 无法对Web API进行身份验证以使用数据?

时间:2017-07-17 10:14:15

标签: dynamics-crm dynamics-crm-webapi

可能是重复的,但由于我没有找到确切的答案,我发布此内容。

我拥有动态CRM Web api& amp;我在我的代码中使用它们如下:

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。

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

我在这里遗漏了什么吗?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

以下是此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"));
  }