You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.5.2在Microsoft Entra ID下的SQL Server连接字符串问题

.NET Framework 4.5.2 Azure Web应用连接Azure SQL Server问题解决方案

数据库提供说明

.NET Framework 4.5.2默认引用的System.Data.SqlClient不支持Microsoft Entra身份验证的Authentication连接字符串参数,该参数是后续版本的Microsoft.Data.SqlClient新增功能。因此你需要向项目中添加Microsoft.Data.SqlClient NuGet包(建议使用v2.0及以上版本,该版本开始全面支持Entra各类认证方式)。Azure中适配SQL Server的主流提供程序包括:

  • System.Data.SqlClient:传统.NET Framework内置提供程序,旧版本无Entra认证支持
  • Microsoft.Data.SqlClient:微软新版SQL客户端,支持Entra全量认证方式
  • System.Data.Odbc/System.Data.OleDb:通用兼容类提供程序,不推荐用于Azure SQL Server场景

各连接字符串错误分析及修复方案

1. ADO.NET(Microsoft Entra无密码身份验证)

连接字符串:

Server=tcp:MyDBServer.database.windows.net,1433;Initial Catalog=MyDB;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication="Active Directory Default";

错误:Invalid value for key 'authentication'.
修复:替换为Microsoft.Data.SqlClient提供程序,安装对应NuGet包;为Web应用启用系统托管身份,并在Azure SQL Server中为该托管身份创建登录名及数据库访问权限。

2. 普通SQL身份验证

连接字符串:

Server=tcp:MyDBServer.database.windows.net,1433;Initial Catalog=MyDB;Persist Security Info=False;User ID=MyUID;Password=MyPwd;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

错误:Cannot open server 'xxx' requested by the login. Client with IP address 'xx.x.xxx.x' is not allowed to access the server...
修复:登录Azure门户进入目标SQL Server防火墙设置,添加Web应用的出站IP地址(可在App Service"属性"页面查看),或开启"允许Azure服务和资源访问此服务器"选项,等待5分钟左右生效后重试。

3. ADO.NET(Microsoft Entra密码身份验证)

连接字符串:

Server=tcp:MyDBServer.database.windows.net,1433;Initial Catalog=MyDB;Persist Security Info=False;User ID=MyUID;Password=MyPwd;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication="Active Directory Password";

错误:One or more errors occurred.
修复:切换为Microsoft.Data.SqlClient提供程序,确保User ID为Microsoft Entra用户的完整邮箱地址,同时确认该用户已被授权访问目标SQL数据库。

4. ADO.NET(Microsoft Entra集成身份验证)(带User ID)

连接字符串:

Server=tcp:MyDBServer.database.windows.net,1433;Initial Catalog=MyDB;Persist Security Info=False;User ID=MyUID;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication="Active Directory Integrated";

错误:Cannot use 'Authentication=Active Directory Integrated' with 'User ID', 'UID', 'Password' or 'PWD' connection string keywords.
修复:删除连接字符串中的User ID参数,替换为Microsoft.Data.SqlClient提供程序;确保Web应用运行身份(托管身份或本地域账户)已被授权访问SQL Server。

5. ADO.NET(Microsoft Entra集成身份验证)(无User ID)

连接字符串:

Server=tcp:MyDBServer.database.windows.net,1433;Initial Catalog=MyDB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication="Active Directory Integrated";

错误:One or more errors occurred.
修复:替换为Microsoft.Data.SqlClient提供程序;为Web应用托管身份授予SQL Server访问权限——在Entra ID中添加SQL DB Contributor角色,或在SQL Server执行以下脚本创建登录名并授权:

CREATE USER [你的Web应用托管身份名称] FROM EXTERNAL PROVIDER;
ALTER ROLE db_datareader ADD MEMBER [你的Web应用托管身份名称];
ALTER ROLE db_datawriter ADD MEMBER [你的Web应用托管身份名称];

内容的提问来源于stack exchange,提问作者Scott Pendleton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 07:58:13