0

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

我有动态 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。

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

我在这里错过了什么吗?

4

2 回答 2

1

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

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;
于 2017-07-18T09:35:36.180 回答
0

这是MSDN 文章中的一个示例,其中包含大量 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"));
  }
于 2017-07-17T14:06:44.137 回答