模拟成功但数据库连接失败(Impersonation succeeds but database connection fails)

编程入门 行业动态 更新时间:2024-10-27 15:29:28
模拟成功但数据库连接失败(Impersonation succeeds but database connection fails)

我有这样的C#(.NET 4.5)代码(用于演示的简化)用于连接到SQL Server 2012数据库:

public static void Test(WindowsIdentity ident) { using (WindowsImpersonationContext ctx = ident.Impersonate()) { using (SqlConnection con = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Persist Security Info=False;Integrated Security=SSPI;Network Library=dbmssocn")) { con.Open(); } ctx.Undo(); } }

Open()方法每次都抛出以下异常:

System.Data.SqlClient.SqlException(0x80131904):建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:TCP提供者,错误:0 - 无法建立连接,因为目标计算机主动拒绝它。)

模仿是成功的,因为如果我添加这样的跟踪:

public static void Test(WindowsIdentity ident) { using (TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\temp\trace.log")) { using (WindowsImpersonationContext ctx = ident.Impersonate()) { listener.WriteLine("Impersonated"); using (SqlConnection con = new SqlConnection("Data Source=MyServer,MyPort;Initial Catalog=MyDatabase;Persist Security Info=False;Integrated Security=SSPI;Network Library=dbmssocn")) { listener.WriteLine("About to open connection; WindowsIdentity.GetCurrent().Name = " + WindowsIdentity.GetCurrent().Name); con.Open(); } ctx.Undo(); listener.WriteLine("Impersonation undone"); } } }

我明白了:

Impersonated About to open connection; WindowsIdentity.GetCurrent().Name = MyDomain\MyUser

如果我将完全相同的连接字符串放在.udl文件中,请在同一台计算机上的相同“MyDomain \ MyUser”帐户下运行它,然后单击“测试连接”,它会成功。 既然如此,我只是看不出问题如何成为防火墙或任何性质的问题。 有谁知道会出现什么问题? 我无权登录数据库服务器本身,因此此时无法检查其事件日志(等)。

I have C# (.NET 4.5) code like this (simplified for demonstration) used to connect to a SQL Server 2012 database:

public static void Test(WindowsIdentity ident) { using (WindowsImpersonationContext ctx = ident.Impersonate()) { using (SqlConnection con = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Persist Security Info=False;Integrated Security=SSPI;Network Library=dbmssocn")) { con.Open(); } ctx.Undo(); } }

The Open() method throws the following exception every time:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

The impersonation is succeeding because if I add tracing like this:

public static void Test(WindowsIdentity ident) { using (TextWriterTraceListener listener = new TextWriterTraceListener(@"C:\temp\trace.log")) { using (WindowsImpersonationContext ctx = ident.Impersonate()) { listener.WriteLine("Impersonated"); using (SqlConnection con = new SqlConnection("Data Source=MyServer,MyPort;Initial Catalog=MyDatabase;Persist Security Info=False;Integrated Security=SSPI;Network Library=dbmssocn")) { listener.WriteLine("About to open connection; WindowsIdentity.GetCurrent().Name = " + WindowsIdentity.GetCurrent().Name); con.Open(); } ctx.Undo(); listener.WriteLine("Impersonation undone"); } } }

I get this:

Impersonated About to open connection; WindowsIdentity.GetCurrent().Name = MyDomain\MyUser

If I put the exact same connection string in a .udl file, run it under the same "MyDomain\MyUser" account on the same machine, and click Test Connection, it succeeds. Since that's the case, I just don't see how the problem could be a firewall or anything of that nature. Does anyone know what could be going wrong? I do not have access to log onto the database server itself, so I cannot check its event log (etc.) at this point.

最满意答案

您的问题来自连接字符串。 连接字符串中的“ 网络库= dbmssocn ”将使客户端尝试连接到UDP端口1434上的SQL服务器而不是TCP端口1433.从应用程序将连接的应用程序连接字符串中删除“ 网络库= dbmssocn ”到SQL服务器成功。

Your problem come from the connection string. The "Network Library = dbmssocn" in connection string will make client attempting to connect to SQL server on the UDP port 1434 rather than the TCP port 1433. You remove the "Network Library = dbmssocn" from the your application's connection string the application will connect to SQL server successfully.

更多推荐

本文发布于:2023-07-20 12:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库连接   Impersonation   succeeds   fails   connection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!