Proper Configuration of SQL Server Connection Strings in ASP.NET Core MVC

Nov 26, 2025 · Programming · 12 views · 7.8

Keywords: ASP.NET Core | Connection String | SQL Server

Abstract: This article provides a comprehensive guide on correctly configuring SQL Server connection strings in ASP.NET Core MVC applications, specifically addressing the common 'Keyword not supported: userid' error when using SQL authentication. It covers the correct syntax format of connection strings, configuration file settings, and code implementation, along with security considerations for protecting sensitive information using tools like Secret Manager.

Connection String Configuration Fundamentals

In ASP.NET Core MVC application development, proper configuration of database connection strings is crucial for ensuring application functionality. When using SQL Server as the backend database with SQL authentication, the syntax format of connection strings becomes particularly important.

Common Error Analysis

Developers often encounter the "ArgumentException: Keyword not supported: 'userid'" error when configuring connection strings. This error stems from incorrect parameter naming in the connection string. SQL Server connection strings require "User Id" (note the space) rather than "userid" to specify the username.

Correct Connection String Format

Based on best practices, the proper format for SQL Server connection strings should be:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;

Key parameter explanations:

Configuration File Setup

In the appsettings.json file, the correct configuration for connection strings is:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;Trusted_Connection=False;MultipleActiveResultSets=true;"
  }
}

Connection String Retrieval in Code

In the Program.cs file, when configuring the database context through dependency injection, use the GetConnectionString method to retrieve the connection string:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") 
    ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));

Security Considerations and Best Practices

Connection strings often contain sensitive information such as usernames and passwords, requiring appropriate security measures:

dotnet user-secrets init
dotnet user-secrets set ConnectionStrings:DefaultConnection "Server=myserver;Database=mydatabase;User Id=id;Password=mypwd"

Error Troubleshooting and Validation

When encountering connection string-related errors, follow these troubleshooting steps:

  1. Verify connection string parameter names are correct (especially the space in "User Id")
  2. Validate the accuracy of server address, database name, username, and password
  3. Confirm network accessibility to the database server
  4. Check if user permissions are sufficient to access the specified database

Conclusion

Proper configuration of SQL Server connection strings is essential for the normal operation of ASP.NET Core MVC applications. By using the correct parameter name "User Id" and combining appropriate configuration file management with security measures, common configuration errors can be avoided, ensuring stable application performance across different environments. Developers should always follow security best practices to properly protect connection strings containing sensitive information.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.