Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to MSSQL Server with windows authentication and Force Encryption set to true

I was connecting to Sql Server 2008 & 2008+ via Java program with

  • Java 8
  • Sql jdbc microsoft driver 4.1
  • Connection string: DriverManager.getConnection("jdbc:sqlserver://<Ip>;instance=MSSQLSERVER;domain=<domain>;IntegratedSecurity=true;ssl=request;", "administrator", "password");

I was able to connect successfully. However when I enabled Force encryption to true in the sql server via sql server configuration manager.

enter image description here I started getting following error.

com.microsoft.sqlserver.jdbc.SQLServerException: An existing connection was forcibly closed by the remote host ClientConnectionId:xxxx

FYI: I have already tried adding below parameters in connection string.

  • ssl=request
  • ssl=require
  • encrypt=true
  • trustServerCertificate=true
  • Also I have tried upgrading the driver to 4.2 & mssql driver 7.0
  • Tried jtds driver as well
  • Point to be noted: I am able to connect to instance via ssms

EDIT 1

- Another important point - It's happening only for windows authentication (enabled via IntegratedSecurity=true; in connection string). So this case is happening only when Force encryption is set to true and we try to connect in windows authentication mode.

like image 441
Vaibhav Jain Avatar asked Nov 10 '18 08:11

Vaibhav Jain


People also ask

How do I connect to SQL Server with Windows authentication?

Open SQL Server Management Studio. In Connect to Server, select Database Engine, enter your SQL Server name, and enter administrator credentials to connect to the server. Select Connect. In Object Explorer, expand the SQL Server, expand Security, right-click Logins, and then select New Login.

How do I connect to SQL Server with SQL authentication?

On the Security page, under Server authentication, select the new server authentication mode, and then click OK. In the SQL Server Management Studio dialog box, click OK to acknowledge the requirement to restart SQL Server. In Object Explorer, right-click your server, and then click Restart.

How do I enable force encryption in SQL Server?

Using SQL Server Configuration Manager, right-click SQL Server Native Client Configuration, and then select Properties. On the Flags page, in the Force protocol encryption box, select Yes.

Can a SQL Server login use Windows authentication?

There are two possible modes: Windows Authentication mode and mixed mode. Windows Authentication mode enables Windows Authentication and disables SQL Server Authentication. Mixed mode enables both Windows Authentication and SQL Server Authentication. Windows Authentication is always available and cannot be disabled.


1 Answers

@Vivien @MarkRotteveel Thanks for your answers which gave me directions.

It turned out that sql server with version less then 11.0 were having troubles and various improvements were done as part of 11.0 version of sql server which rectified errors of windows authentication + encrypted connections connectivity.

enter image description here

So Sql server 2012 and above will support windows authentication with TLSv1.2, windows authentication along with encrypted connections seamlessly. Now I am using

  • Java 8
  • Sql server driver 4.2
  • Windows 10
  • No extra parameters in connection string.

Just for reference: Found on Microsoft website. enter image description here

like image 128
Vaibhav Jain Avatar answered Sep 23 '22 14:09

Vaibhav Jain