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:
Server: Specifies the database server addressDatabase: Specifies the target database nameUser Id: Specifies the login username (note the space)Password: Specifies the corresponding password
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:
- Use
appsettings.Development.jsonfor connection strings in development environments - For production environments, recommend using environment variables or secure storage like Azure Key Vault
- Utilize the Secret Manager tool for sensitive information management:
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:
- Verify connection string parameter names are correct (especially the space in "User Id")
- Validate the accuracy of server address, database name, username, and password
- Confirm network accessibility to the database server
- 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.