【发布时间】:2016-10-20 01:24:54
【问题描述】:
我开发的应用程序必须由公司中的任何人使用,但没有任何人有权访问数据库,因此我冒充具有足够权限访问数据库部分的用户。但是,尽管我使用具有足够权限的用户名和密码,即使我的个人帐户具有足够权限,我也会收到身份验证错误
用户 '' 登录失败。
有什么想法吗?下面你可以看到我的代码。
PS1:我使用Matt Johnson's library 进行模拟,正如here 所述。
PS2:在连接字符串中我使用的是Integrated Security=false。当我使用Integrated Security=SSPI时,错误信息是
登录失败。登录来自不受信任的域,不能用于 Windows 身份验证。
代码:
public static void test(string domain, string username, string password, LogonType type)
{
string connectionString = @"Data Source=testsqlserver.company.com;Initial Catalog=TABLENAME;Integrated Security=false;Connection Timeout=240";
using (Impersonation.LogonUser(domain, username, password, type))
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlConnection.Open();
try
{
// Still not reached this point
}
catch (Exception exception)
{
string exceptionMessage = "Exception message:\n" + exception.Message.ToString() + "\nEnd of the exception message.";
Console.WriteLine(exceptionMessage);
}
sqlConnection.Close();
}
}
}
【问题讨论】:
-
您的 SQL Server 是否支持 sql 身份验证?如果确实如此,请改用它。
标签: c# sql-server exception impersonation