C# – Exception in salesforce login with valid credentials
Exception thrown: ‘Salesforce.Common.ForceAuthException’ in Salesforce.Common.dll
Exception thrown: ‘Salesforce.Common.ForceAuthException’ in mscorlib.dll
I am using oauth-rest api to login into salesforce. I have passed valid consumer key, secret, username and password, still it gives above error. On reading above stack trace, it looks like we must have sent invalid credentials but this is not the case. Problem is with the security protocol.
For .NET, default is TLS1.0. On calling salesforce through rest api, it expect it to be TLS1.1 or higher since there is already a setting activated inside it (salesforce). This part contradicts and does not let us login even with valid credentials.
Go to Critical updates, deactivate “ Require TLS 1.1 or higher for HTTPS connections “ in your salesforce org.
Better way : Add below line before calling asynchronous login method in your code to explicitly change the protocol type:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var auth = new AuthenticationClient();
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url);
You can change above setting to below when TLS1.0 is required:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;