ADO.NET  

In-Depth Look at Advanced Authentication and Connection Strings for SQL Server and Databases (Part II)

Part I - https://www.c-sharpcorner.com/article/c-sharp-net-advanced-authentication-types-for-sql-server-and-other-database-syste/

Connection strings define how your application connects to the database. Below are examples of the most common authentication mechanisms used in modern SQL Server environments.

ChatGPT Image Oct 22, 2025, 08_45_38 AM

⚙️ SQL Server Connection String Properties Explained

A connection string is a key component in any application that connects to SQL Server. It defines how the connection should be established, which credentials to use, and what configuration applies to the session. Below are the most common and essential properties used in SQL Server connection strings.

1. Data Source (or Server)

Specifies the server name or network address of the SQL Server instance.
It can include the server name, instance name, or IP address.

Examples:

Data Source=SQLSERVER01;
 Data Source=SQLSERVER01\DEV;
 Data Source=192.168.1.15,1433;

Explanation:

  • SQLSERVER01 → Default instance on a machine.

  • SQLSERVER01\DEV → Named instance.

  • ,1433 → Specifies a custom TCP port (default is 1433).

2. Initial Catalog (or Database)

Specifies the default database to connect to after login.

Example:

Initial Catalog=InventoryDB;

If omitted, SQL Server connects to the user’s default database defined in their login profile.

3. User ID

The username for SQL Server Authentication.

Example:

User ID=AppUser;

Only required for SQL Authentication. Not used for Windows or Azure AD Integrated Authentication.

4. Password

The password associated with the SQL Server login.

Example:

Password=MyStrongPassword123;

Tip:
Always use encrypted configuration files or secure vaults (e.g., Azure Key Vault, AWS Secrets Manager) to store credentials.

5. Integrated Security / Trusted_Connection

Determines whether to use Windows Authentication instead of SQL Authentication.

Example:

Integrated Security=SSPI;

or

Trusted_Connection=True;

Explanation:

  • When enabled, SQL Server uses the Windows identity of the user running the application.

  • SSPI stands for Security Support Provider Interface, which handles the authentication handshake with Windows.

6. Persist Security Info

Controls whether sensitive data (like username and password) remains available after a connection is opened.

Example:

Persist Security Info=False;

Best Practice: Always set to False to prevent exposing credentials through code or logs.

7. Connect Timeout

Specifies how many seconds to wait while trying to establish a connection before timing out.

Example:

Connect Timeout=15;

Default: 15 seconds
Increase this value for slow networks or remote servers.

8. Encrypt

Forces encryption of the communication channel between the client and SQL Server.

Example:

Encrypt=True;

Explanation:
When enabled, the data transmitted is encrypted using TLS (Transport Layer Security), preventing packet sniffing and data interception.

9. TrustServerCertificate

Used with Encrypt=True.
If set to True, the client will trust the SQL Server’s certificate without validation.

Example:

TrustServerCertificate=True;

Best Practice:
Use only in development or when using self-signed certificates.
In production, always use a valid SSL certificate.

10. Application Name

Specifies the name of the application connecting to SQL Server.
Useful for monitoring and auditing connections.

Example:

Application Name=OrderProcessingApp;

You can view active connections in SQL Server using:

SELECT program_name FROM sys.dm_exec_sessions;

11. MultipleActiveResultSets (MARS)

Allows multiple batches or queries to be executed on the same connection simultaneously.

Example:

MultipleActiveResultSets=True;

Useful when executing several queries at once in Entity Framework or ADO.NET.

12. Encrypt / Authentication (Azure AD Specific)

For Azure SQL Database, additional authentication keywords are used.

Examples:

Authentication="Active Directory Password";
Authentication="Active Directory Integrated";
Authentication="Active Directory Managed Identity";

These properties define the authentication mode for Azure AD–based connections.

13. Network Library (Optional)

Specifies the network protocol for SQL Server connections.

Example:

Network Library=dbmssocn;  -- TCP/IP

Other options:
dbnmpntw (Named Pipes), dbmsrpcn (RPC), etc.
In most cases, TCP/IP is the preferred and default protocol.

Example—Combined Connection Strings

SQL Authentication:

Data Source=SQLSERVER01;Initial Catalog=OrdersDB;
User ID=OrderApp;Password=StrongP@ss123;
Persist Security Info=False;Encrypt=True;TrustServerCertificate=False;
Connect Timeout=30;Application Name=OrderApp;

Windows Authentication:

Data Source=SQLSERVER01;Initial Catalog=OrdersDB;
Integrated Security=SSPI;Encrypt=True;Connect Timeout=15;

Azure AD Managed Identity:

Server=tcp:myserver.database.windows.net,1433;
Database=MyCloudDB;
Authentication="Active Directory Managed Identity";
Encrypt=True;

Summary: Common SQL Server Connection String Properties

PropertyPurposeApplies ToRecommended Setting / Notes
Data SourceSpecifies SQL Server name or IP address (optionally with instance).AllUse server name or FQDN; include port if non-default (e.g., ,1433).
Initial CatalogDefines the default database to connect to.AllAlways specify explicitly to avoid connecting to the default user DB.
User IDSQL login username.SQL AuthenticationUse for application accounts; avoid hardcoding.
PasswordSQL login password.SQL AuthenticationStore securely using secrets vaults or environment variables.
Integrated Security / Trusted_ConnectionUses Windows Authentication instead of SQL login.Windows AuthenticationRecommended for domain environments (no password exposure).
Persist Security InfoControls whether sensitive info (e.g., password) remains available after connection.AllAlways set to False for security.
Connect TimeoutTime (in seconds) to wait for the connection to establish.AllDefault is 15; increase for remote DBs.
EncryptEnables TLS encryption for the connection.AllAlways set to True, especially in cloud or external connections.
TrustServerCertificateTrusts the server’s SSL certificate without validation.AllUse only in dev/test with self-signed certs.
Application NameIdentifies the app in SQL Server sessions.AllUse descriptive names for auditing and diagnostics.
MultipleActiveResultSets (MARS)Allows multiple queries on one connection.AllUse with ORM frameworks like EF when needed.
AuthenticationSpecifies Azure AD authentication mode.Azure SQL / ADUse “Active Directory Integrated” or “Managed Identity” when available.
Network LibraryDefines network protocol (e.g., TCP/IP).On-premises / legacyDefault is TCP; rarely changed in modern setups.

Best Practices

  • Use Windows or Azure AD Authentication wherever possible.

  • Always enable encryption (Encrypt=True).

  • Avoid storing credentials in plain text—use secure secrets management.

  • Include the application name for observability in multi-app environments.

  • Test connections with SqlConnection.ConnectionString validation tools before deployment.