13 November 2017

D365fO | OData - An existing connection was forcibly closed by the remote host 10054


As soon as you connect to 2 Tier environments SAT or PROD environments you should keep in mind to connect using TLS 1.2. The easiest way to do this is compiling with .Net 4.6 or higher

If you are using the sample service scripts you can just add the following line of code into OAuthHelper.cs


         public static string GetAuthenticationHeader()
        {
            string aadTenant = ClientConfiguration.Default.ActiveDirectoryTenant;
            string aadClientAppId = ClientConfiguration.Default.ActiveDirectoryClientAppId;
            string aadResource = ClientConfiguration.Default.ActiveDirectoryResource;

            AuthenticationContext authenticationContext = new AuthenticationContext(aadTenant);
            AuthenticationResult authenticationResult;

            // TLS 1.2
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

           
            if (useWebAppAuthentication)
            {
                string aadClientAppSecret = ClientConfiguration.Default.ActiveDirectoryClientAppSecret;
                var creadential = new ClientCredential(aadClientAppId, aadClientAppSecret);
                authenticationResult = authenticationContext.AcquireTokenAsync(aadResource, creadential).Result;
            }
            else
            {
                // OAuth through username and password.
                string username = ClientConfiguration.Default.UserName;
                string password = ClientConfiguration.Default.Password;

                // Get token object
                var userCredential = new UserPasswordCredential(username, password);
                authenticationResult = authenticationContext.AcquireTokenAsync(aadResource, aadClientAppId, userCredential).Result;
            }

            // Create and get JWT token
            return authenticationResult.CreateAuthorizationHeader();


Error Messages:

InnerException {"An existing connection was forcibly closed by the remote host"} 

ErrorCode 10054
Message "An existing connection was forcibly closed by the remote host"
Message "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host." 

No comments: