Trouble Connecting to sql server Login failed. "The login is from an untrusted domain and cannot be used with Windows authentication"

I am trying to host a SQL server database, but whenever I try to connect to it I get this error:

The login is from an untrusted domain and cannot be used with Windows authentication

I am connecting through Matlab using the following command:

conn = database('Clinical_Data','DoyleLab07\Acc','','com.microsoft.sqlserver.jdbc.SQLServerDriver','jdbc:sqlserver://DOYLELAB07\SQLEXPRESS:54287;database=Clinical_Data;integratedSecurity=true;').

Connecting to the database using matlab worked fine as long as I was using matlab on the computer which I was using to host the server. However, when I use another computer and the same Matlab command I get the error I showed above.

When I look under control panel\system. I notice that no domain is listed on my host PC or the PC I am using to connect to the host, but both computers are in the same workgroup. Would I be able to fix my problem by creating a domain and adding the foreign PC and the host to that domain? If so, how can this be accomplished?

Any suggestions will be very much appreciated. Thank you for reading my post.


Solution 1:

Getting rid of Integrated Security=true worked for me.

Solution 2:

In order to use Windows Authentication one of two things needs to be true:

  1. You are executing from the same machine as the database server.
  2. You have an Active Directory environment and the user the application is executing under (usually the logged in user) has rights to connect to that database.

If neither of those are true you have to do one of two things:

  1. Establish a Windows Domain Controller, connect all of the relevant machines to that controller, then fix SQL server to use domain accounts; OR,
  2. Change SQL server to use both Windows and SQL Server accounts.

By FAR the easiest way is to change SQL Server to use both Windows and SQL server accounts. Then you just need to create a sql server user on the DB server and change your connection string to do that.

Best case option 1 will take a full day of installation and configuration. Option 2 ought to take about 5 minutes.

Solution 3:

If your SQL Server is on one domain controller and you are trying to connect to it from another domain controller then you will get this error when

IntegratedSecurity = true;

This will happen even if you include a valid SQL Server username and password in your connection string as they will automatically be over-written with your windows login and password. Integrated security means simply - use your windows credentials for login verification to SQL Server. So, if you are logged in to a different domain controller then it will fail. In the case where you are on two different domain controllers then you have no choice but to use

IntegratedSecurity = false;

Now, when Integrated security is false SQL Server will use the SQL Server login and password provided in your connection string. For this to work, the SQL Server instance has to have its authentication mode configured to mixed mode, being, SQL Server and Windows Authentication mode.

To verify or change this setting in SQL Server you can open the SQL Server Management Studio and right-click on your server name and then select Properties. On the pop-up that appears select Security and you will see where to alter this setting if you need to.

Solution 4:

I've had this same issue when using DNS aliases and hosts files to connect to a machine using a different domain name.

Say you have a SQL server called sql1 on mydomain.com - which is an Active Directory domain - and you also have a DNS zone for mydomain.net, and - for consistency - you set up a DNS alias (CNAME) record for database.mydomain.net --> sql1.mydomain.com

You'll be able to connect to sql1.mydomain.com using Windows integrated security, but won't be able to connect to database.mydomain.net even though it's the same server because the domain name doesn't match your AD domain.

Solution 5:

This error message can also occur if the account you are using to access the SQL server is locked out by the domain.