【发布时间】:2021-09-20 13:05:08
【问题描述】:
我有一个使用 匿名身份验证 运行的 Web 应用程序和一个到具有专用 SQL 帐户的 SQL 服务器的连接字符串。连接字符串:
<add name="ConnectionString" connectionString="Data Source=xxxxx;Initial Catalog=xxxxx;User ID=xxxxxx;Password=xxxxxx" providerName="System.Data.SqlClient" />
一切正常,但在向应用程序添加访问控制后:
<system.webServer>
<security>
<authorization>
<remove users="*" roles="" verbs="" />
<add accessType="Allow" roles="DOMAIN\ADGroup" />
</authorization>
</security>
</system.webServer>
并在 IIS 中更改为 Windows 身份验证,使用 SQL 服务器连接的页面停止工作。我没有更改连接字符串。错误信息:
发生网络相关或特定于实例的错误,同时 建立与 SQL Server 的连接。找不到服务器或 无法访问。验证实例名称是否正确,并且 SQL Server 配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 定位服务器/实例时出错 指定的) 源错误:
在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈跟踪:
[SqlException (0x80131904): 网络相关或实例特定 建立与 SQL Server 的连接时出错。这 服务器未找到或无法访问。验证实例 名称正确且 SQL Server 配置为允许远程 连接。 (提供者:SQL 网络接口,错误:26 - 错误 定位服务器/指定实例)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔型 breakConnection) +6318697
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +245 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, 布尔值 ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert,布尔集成安全,SqlConnection owningObject,带有故障转移的布尔值)+536
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,字符串 newPassword,布尔型 ignoreSniOpenTimeout,Int64 timerExpire, SqlConnection owningObject, Boolean withFailover) +283
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(字符串 主机,字符串 newPassword,布尔重定向用户实例, SqlConnection owningObject、SqlConnectionString connectionOptions、 Int64 timerStart) +6338834
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection 拥有对象,SqlConnectionString 连接选项,字符串 newPassword,布尔重定向用户实例)+6338792
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 身份、SqlConnectionString 连接选项、对象 providerInfo、 字符串 newPassword、SqlConnection owningObject、布尔值 重定向用户实例)+354
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项,对象 poolGroupProviderInfo,DbConnectionPool 池, DbConnection owningConnection) +300
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +45
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection 拥有连接)+6343166
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection externalConnection, DbConnectionFactory connectionFactory) +6343479
System.Data.SqlClient.SqlConnection.Open() +258
System.Web.Management.SqlServices.GetSqlConnection(字符串服务器, 字符串用户、字符串密码、布尔值信任、字符串 连接字符串)+115[HttpException (0x80004005): 无法连接到 SQL Server 数据库。]
System.Web.Management.SqlServices.GetSqlConnection(字符串服务器, 字符串用户、字符串密码、布尔值信任、字符串 连接字符串)+4052597
System.Web.Management.SqlServices.SetupApplicationServices(字符串 服务器,字符串用户,字符串密码,布尔值信任,字符串 连接字符串、字符串数据库、字符串 dbFileName、SqlFeatures 功能,布尔安装)+159
System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(字符串 fullFileName, String dataDir, String connectionString) +825
连接字符串的使用示例:
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
有什么可能导致这种情况的想法吗?
【问题讨论】:
-
你为 SQL 添加 TCP/UDP 端口规则了吗?
-
@EminNiftiyev 嗨,我不知道为什么我应该改变这样的东西?一旦我回到 IIS 中的 web 应用程序上的匿名身份验证,一切都会再次运行。我想要的只是限制对已发布 Web 应用程序的访问。我无法访问 SQL 服务器。
-
@EminNiftiyev 我没有关注你。与 SQL 数据库的连接工作正常。当我更改 IIS 中的身份验证设置时,它停止工作。
-
在堆栈跟踪中使用
CreateMdfFile,它似乎正在尝试连接到(LocalDb)实例。如果你改用Data Source=tcp:YourServerNameOrIpAddress;,它会起作用吗? -
您能否确认您使用的是 Web 服务器上的本地 .MDF 文件,而不是远程 SQL Server? .MDF 文件可能用于 ASP.NET 标识,而不是您的主数据库。
标签: sql-server iis connection-string