TLS 1.2 in Azure Function connecting to Dynamics 365

July 2017 update for Dynamics 365 (online) requires connections to customer engagement applications to utilize TLS 1.2 (or better) security.

If you are connecting to July 2017 update for Dynamics 365 (online) from an Azure Function using Xrm Tooling dlls, you may experience error being returned, "Unable to login to dynamics crm ...". This is because the .NET Framework version of Azure Function (C#) is 4.6.1, and by default it doesn't use TLS 1.2.

To fix this, just add ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; before you initialize the CrmServiceClient object.

using System;
using System.Configuration;
using System.Net;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.ServiceBus.Messaging;
using Microsoft.Xrm.Tooling.Connector;


namespace GymSports.Azure.Integration
{
    public static class Contacts
    {
        [FunctionName("Contacts")]
        public static void Run([ServiceBusTrigger("contacts", AccessRights.Listen, Connection = "Integration_Receiver_SERVICEBUS")]string myQueueItem, TraceWriter log)
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
               
                CrmServiceClient crmSvc = new CrmServiceClient(ConfigurationManager.ConnectionStrings["crmConn"].ConnectionString);

                if (crmSvc != null && crmSvc.IsReady)
                {
                    log.Info($"Connected to CRM instance {crmSvc.ConnectedOrgFriendlyName}.");
                }
                else
                    log.Info(crmSvc.LastCrmError + " ... " + crmSvc.LastCrmException);
            }
            catch(Exception ex)
            {
                log.Info(ex.ToString());
            }
        }
    }
}

Comments

Popular posts from this blog

Dynamics 365 sub-grid add new and add existing

Dynamics 365 Web API get entity using alternate key value that has apostrophe/single quote in it

TLS 1.2 and PowerShell