小编典典

从网络目录运行可执行文件时 C# SQL Server 集成安全性错误,从 exe 的本地副本运行时工作正常

sql

我正在使用以下 C# 代码查询 SQL Server 数据库:

SqlConnectionStringBuilder sqlCSB = new SqlConnectionStringBuilder();
sqlCSB["Data Source"] = @"SERVER\MSSQL";
sqlCSB.Remove("User ID");
sqlCSB.Remove("Password");
sqlCSB["integrated Security"] = true;
sqlCSB["Initial Catalog"] = "InitCatalog";

using (SqlConnection connection = new SqlConnection(sqlCSB.ConnectionString))
{
    using (SqlCommand cmd = connection.CreateCommand())
    {
        cmd.CommandText = "SELECT * FROM public.v_Data;";
        MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
        // this shows the correct (same) user regardless from which path (remote or local) the App is running from
        connection.Open();

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            int ord = reader.GetOrdinal("ColumnA");

            while (reader.Read())
            {
               returnedData.Add(reader.GetString(ord).Substring(2));
            }
         }
      }
   }

   return returnedData;
}

当应用程序从本地路径(例如C:\myapp.exe)启动时,一切正常,但是当我尝试从例如运行它时Netdrive (\\\filer) (Z:),我收到以下错误:

System.Data.SqlClient.SqlException (0x80131904): (provider: SQL Network Interfaces, error: 26)

at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Main.refresh() in Main.cs:Line 397. // connection.Open();

ClientConnectionId:00000000-0000-0000-0000-000000000000 (Error Number):-1,(State):0,(Class):20

根本原因是什么?我不确定“集成安全性”和从网络目录启动应用程序之间的某些交互是否会导致错误。

如上所述,它始终是同一个 Windows 用户,并且该帐户可以访问网络驱动器。

在这里调试的最佳方法是什么?


阅读 122

收藏
2022-07-22

共1个答案

小编典典

我的问题的答案可以在这里找到:SqlConnection Error if EXE is executed from network path

“最后我发现了问题:在具有共享文件夹的服务器中,SMBv2 被禁用(我不知道为什么),因此只有 SMBv1 处于活动状态;相同的程序从同一网络中的同一客户端执行,但位于具有启用 SMBv2 工作正常。所以问题是关于 SMBv1 共享,从 Windows 10 1803 开始​​已弃用”

2022-07-22