Search code examples
c#oauth-2.0exchangewebservices

How to use 1.2 TLS version in .NET for getting authenticated to EWS Server


First of am very newbie to C# development and trying to switch the authentication from basic to oauth based one. However while testing below code I got depreciated exception.

#Implementation

  // Using Microsoft.Identity.Client
            var cca = ConfidentialClientApplicationBuilder
                .Create(clientId)      //client Id
                .WithClientSecret(clientSecret)
                .WithTenantId(tenantId)
                .Build();
            var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
            try
            {
                // Get token
                var authResult = await cca.AcquireTokenForClient(ewsScopes)
                    .ExecuteAsync();
                this.token = authResult.AccessToken;
            }
            catch (MsalException ex)
            {
                Console.WriteLine($"Error acquiring access token: {ex}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex}");
            }

#Exception


Microsoft.Identity.Client.MsalServiceException
  HResult=0x80131500
  Message=AADSTS1002016: You are using TLS version 1.0, 1.1 and/or 3DES cipher which are deprecated to improve the security posture of Azure AD. Your TenantID is: 9XXXXXXXXXXXXXXXXXXf. Please refer to https://go.microsoft.com/fwlink/?linkid=2161187 and conduct needed actions to remediate the issue. For further questions, please contact your administrator.
Trace ID: 57531a5a-2797-4f77-bc73-11b1e4355800
Correlation ID: 4295ecdd-7aa1-458f-8e6a-03fda78ec30f
Timestamp: 2022-07-25 03:32:33Z
  Source=Microsoft.Identity.Client

I tried using ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

Unfortunately this not working. It would be greatly appreciated if someone could navigate for resolving above exception.


Solution

  • Maybe this could help :

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    

    Add this line where your code launch (the line must be reached as soon as possible), and don't borther adding | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

    It worked for me.