Blog Post

When do usernames and passwords belong in connection strings

,

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]/

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.

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating