When do usernames and passwords belong in connection strings

Leave a comment

December 18, 2017 by Kenneth Fisher

tl;dr: When using a windows or active directory authenticated id you do not put the username or password into your connection string.

Quite possibly the most common question I get is “Why won’t my login connect to the database?”. And the most common response? You’re using it wrong.

In SQL Server there are two types of ids. Windows authenticated and SQL Server authenticated. Typically you can tell the difference because windows authenticated ids have a domain attached.

[domain name]/[user name]

Note: This won’t always be the case. When creating a database level principal (a user) it is possible (in older versions of SQL Server) to create the user with a different name from the server principal (the login) and sometimes the domain name gets removed. It is also possible to create a SQL Server authenticated login/user with what looks like a domain name. So how do you tell for sure? You can either look in the GUI

Or in sys.server_principals or sys.database_principals. The type and type_desc columns will tell you what the actual type of the principal (login or user) is. You can look in BOL for all of the possible options but the ones we care about are:

  • S – SQL Login/User
  • U – Windows/AD Login/User

Here is the meat of it. With a SQL Login you have to pass the user name and password to SQL. With a windows authenticated connection you don’t! For a Windows Authenticated user/group you typically put something like Trusted_Connection (depending on the type of connection string). With Windows Authenticated principals Windows has authenticated your connection (go figure, right?). It then passes that authentication on to SQL Server without requiring it to send the password across the wire. Hence the word trusted.

For help with connection strings in general you can go to this site. There are entries for OLE DB, ODBC and more. There are also (lots and lots of) entries for non SQL Server connection strings if you need them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,753 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: