C# OAuth Call to login.MicrosoftOnline.com Results in TLS/SSL Error Sporadically

Siward 0 Reputation points
2025-12-02T18:03:12.0833333+00:00

Hi Microsoft Q&A,

I wrote a C# application that attempts to get an OAuth token from https://login.microsoftonline.com. The resulting access token is in turn used to call a Microsoft D365 Cloud application web service.

My problem is the my C# application is randomly throwing the below SSL error when it attempts to refresh the OAuth token from https://login.microsoftonline.com:

System.Security.Authentication.AuthenticationException:  The remote certificate is invalid according to the validation procedure.

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.


The SSL error appears randomly. Some connections connect successfully, but others fail with an SSL certificate error.

It feels like login.microsoftonline.com is a load balanced server and one or more of its nodes has a different ssl that my system cannot accept. If my C# calls the correct server on the load balancer node, it connects fine, but if it happens to call the bad load balanced server node, it fails.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.