Andrey Bushman
Andrey Bushman

Reputation: 12516

Linux: How to connect to CRM 365 through Microsoft.PowerPlatform.Dataverse.Client.Dynamics package?

CRM Dynamics 365 v9.1 on-premise.

Linux, Mac OS

Microsoft provides official NuGet packages for Dynamics 365 v9.x here. I have used this for several years. But they are for .Net Framework only (not for .Net Core/5/6).

Also exists official Microsoft.PowerPlatform.Dataverse.Client.Dynamics package. It is preview still but it can be used for .Net Core/5/6 also (according it's documentation).

I write some .Net 6 applications that communicate with CRM Dynamics 365 v.9. They will be published into the docker images later.

Right now I organized the communication via the Dynamics Web API: I create HttpClient with NTLM authentication. For connection I use login and password (they are stored in the system environment variables). It works fine on Mac OS:

static HttpClient CreateCrmHttpClient(string domain, string crmWebApiUrl, string authType, string crmLogin, 
    string crmPassword, Guid? callerId)
{
    var uri = new Uri(crmWebApiUrl);
    var credentialsCache = new CredentialCache
        {{uri, authType, new NetworkCredential(crmLogin, crmPassword, domain)}};

    var handler = new HttpClientHandler {Credentials = credentialsCache};

    var httpClient = new HttpClient(handler) {BaseAddress = uri, Timeout = new TimeSpan(0, 2, 0)};

    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
    httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
    httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations=\"*\"");

    if (callerId != null)
    {
        httpClient.DefaultRequestHeaders.Add("MSCRMCallerID", callerId.Value.ToString());
    }
    return httpClient;
}

Using example:

string domain = "MyCompany";
string crmWebApiUrl = "https://crm-dev.MyCompany.ru:456/MyCompany/api/data/v9.1/";
string crmLogin = Environment.GetEnvironmentVariable("CrmLogin");
string crmPassword = Environment.GetEnvironmentVariable("CrmPassword");
Guid callerId = Guid.Parse("81CF7EFF-A996-44C2-8710-06E8177586C2"); // J.Smith
string authType = "NTLM";    

using (var httpClient = CreateCrmHttpClient(domain, crmWebApiUrl, authType, crmLogin, crmPassword, callerId)) {
  // ...
}

Also I want to learn to communicate with CRM throuh Microsoft.PowerPlatform.Dataverse.Client.Dynamics package using. But I have a problem with connecting to CRM. It doesn't provide capability to connect to CRM by NTLM authentication. I tried to connect with other different methods but unsuccessfully (I learned it's examples).

I need working code-example of connecting to CRM Dynamics 365 v9.1 on-premise through Microsoft.PowerPlatform.Dataverse.Client.Dynamics package using. I mean code example that works Linux or Mac.

Anybody has such experience? I would really appreciate a simple working code example!

Thank you very much.

Upvotes: 0

Views: 1401

Answers (1)

user1886205
user1886205

Reputation: 1

Based on my understanding, from April 1, 2022, Microsoft has already made some changes to their authentication methods to the Microsoft Dataverse. They no longer. Your authType wouldn't work anymore. See the second link for reference. I personally have use ClientSecret and it connects fine with Dynamics 365 SDK Version 9.1 or Microsoft.PowerPlatform.Dataverse.Client Nuget Package Version 1.0.1. You can try AD instead of NTLM as per second link for valid options for the authType parameter.

Reference: Use of Office365 authentication with Microsoft Dataverse

Reference: Use connection strings in XRM tooling to connect to Microsoft Dataverse

Upvotes: -1

Related Questions