cloud config_error ai_generated true

System.InvalidOperationException: The ConnectionString 'DefaultConnection' has an invalid format. Expected format: 'Server=tcp:...;Database=...;User ID=...;Password=...;'

ID: cloud/azure-app-service-connection-string-format

Also available as: JSON · Markdown · 中文
92%Fix Rate
84%Confidence
1Evidence
2024-03-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Azure App Service Windows active
Azure App Service Linux active
.NET 8 active

Root Cause

Azure App Service connection strings stored in app settings often have incorrect syntax (e.g., missing semicolons, wrong key names, or using non-SQL Server format for SQL Database).

generic

中文

Azure App Service 应用设置中存储的连接字符串通常语法错误(例如缺少分号、键名错误或对 SQL 数据库使用了非 SQL Server 格式)。

Official Documentation

https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references

Workarounds

  1. 95% success Use the correct format for Azure SQL Database in app settings: Server=tcp:<server>.database.windows.net,1433;Database=<db>;User ID=<user>@<server>;Password=<password>;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
    Use the correct format for Azure SQL Database in app settings: Server=tcp:<server>.database.windows.net,1433;Database=<db>;User ID=<user>@<server>;Password=<password>;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
  2. 90% success Retrieve the connection string from the Azure Portal: go to your SQL Database -> Connection strings -> ADO.NET, then copy the exact string and paste it into App Service's 'Connection strings' section, selecting 'SQLAzure' as the type.
    Retrieve the connection string from the Azure Portal: go to your SQL Database -> Connection strings -> ADO.NET, then copy the exact string and paste it into App Service's 'Connection strings' section, selecting 'SQLAzure' as the type.
  3. 85% success Use a Key Vault reference with proper syntax: @Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/myconnectionstring/). Ensure the App Service has 'Key Vault Secrets User' role on the vault.
    Use a Key Vault reference with proper syntax: @Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/myconnectionstring/). Ensure the App Service has 'Key Vault Secrets User' role on the vault.

中文步骤

  1. 在应用设置中使用适用于 Azure SQL 数据库的正确格式:Server=tcp:<server>.database.windows.net,1433;Database=<db>;User ID=<user>@<server>;Password=<password>;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
  2. 从 Azure 门户获取连接字符串:转到 SQL 数据库 -> 连接字符串 -> ADO.NET,复制确切字符串并粘贴到应用服务的 '连接字符串' 部分,类型选择 'SQLAzure'。
  3. 使用正确的语法引用 Key Vault:@Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/myconnectionstring/)。确保应用服务在保管库上具有 'Key Vault Secrets User' 角色。

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Adding extra quotes around the connection string in app settings causes parsing errors.

  2. 80% fail

    Using Key Vault references without proper syntax like @Microsoft.KeyVault(SecretUri=...) results in the raw reference string being used as the connection string.

  3. 85% fail

    Copying a connection string from SQL Server Management Studio that includes 'Trusted_Connection=True' fails because Azure SQL requires SQL authentication.