【问题标题】:How to Transact data from my API to Dynamic 365 API using c#如何使用 c# 将数据从我的 API 传输到 Dynamic 365 API
【发布时间】:2018-08-17 18:40:20
【问题描述】:

我的要求是将我的数据库记录同步到 Dynamic 365 并对用户进行身份验证。

我正在尝试使用此代码,但它没有帮助我,请帮助我连接并解决它

string api = "https://ujwl.api.crm8.dynamics.com/api/data/v9.0";

AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(api)).Result;

var creds = new ClientCredential("xxx", "xxx");


AuthenticationContext authContext = new AuthenticationContext(ap.Authority);

var token = authContext.AcquireTokenAsync(ap.Resource, creds).Result.AccessToken;

using (HttpClient httpClient = new HttpClient())
{
httpClient.Timeout = new TimeSpan(0, 2, 0);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

HttpResponseMessage response = await httpClient.GetAsync("https://ujwl.api.crm8.dynamics.com/api/data/v9.0/accounts?$top=2");
}

我无法验证用户身份,也无法从服务器获取数据。 这种方法是否正确,或者我需要采用不同的方法。

【问题讨论】:

    标签: c# oauth-2.0 dynamics-crm dynamic-programming


    【解决方案1】:

    这是我通过挖掘 Microsoft Docs 得到的上述问题的解决方案

    string resource = "https://ujwl.crm8.dynamics.com/";
    string clientId = "aaaa1111-xxxx-yyyy-zzzz-2222bbbbc692";
    string redirectUrl = "https://localhost:44345/";
    
    AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/common", false);
    AuthenticationResult result = authContext.AcquireToken(resource, clientId, new Uri(redirectUrl));
    HttpResponseMessage response = null;
    using (HttpClient httpClient = new HttpClient())
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
        httpClient.Timeout = new TimeSpan(0, 2, 0);
        httpClient.BaseAddress = new Uri("https://ujwl.crm8.dynamics.com/");
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
    
        response = await httpClient.GetAsync("https://ujwl.api.crm8.dynamics.com/api/data/v9.0/contacts");
    }
    

    【讨论】:

      【解决方案2】:

      我只在本地完成,所以我可能是错的......但在我看来你可以/应该使用 MS c# API。命名空间为Microsoft.Xrm.Sdk,可以在MS页面下载。

      请注意,以下代码是部分代码,对于内部部署,可能在线会有不同的连接字符串/参数。

              private IOrganizationService _orgService = null;
      
              public CrmHelper(string serverHost, int port, string orgName, bool usarSSL, string dominio, string usuario, string password, bool claims)
              {
                  /* Nomenclatura usando CLAIMS
       * ServiceUri=https://[server:puerto]/[NombreOrganizacion];
       * AuthType=IFD;
       * Domain=[Dominio];
       * UserName=[Dominio]\[usuario];
       * Password=[Password];
       * LoginPrompt=Never;
       * Organization=[NombreOrganizacion] */
      
                  /* Nomenclatura sin usar CLAIMS: 
       * Url=http://[direccionIP]/[NombreOrganizacion]; 
       * Domain=[Dominio]; 
       * Username=[usuario]; 
       * Password=[Password]; 
       * authtype=AD 
                   */
                  string formato = claims ? @"ServiceUri={0}://{1}{2}/{3};AuthType=IFD;Domain={4};UserName={4}\{5};Password={6};LoginPrompt=Never;Organization={3}" : @"Url={0}://{1}{2}/{3}; Domain={4}; Username={5}; Password={6}; authtype=AD";
                  string connectionString = string.Format(formato, (usarSSL) ? "https" : "http", serverHost, (port == 80) ? "" : ":" + port.ToString(), orgName, dominio, usuario, password);
                  CrmServiceClient conn = new Xrm.Tooling.Connector.CrmServiceClient(connectionString);
      
                  // Cast the proxy client to the IOrganizationService interface.
                  _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
      
                  if (_orgService == null || !conn.IsReady)
                  {
                      throw new ArgumentException("No se ha podido conectar al CRM con los parametros facilitados");
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2021-07-15
        • 2021-07-01
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 1970-01-01
        • 2018-10-03
        相关资源
        最近更新 更多