【问题标题】:SQL Server : error when impersonating user in C#SQL Server:在 C# 中模拟用户时出错
【发布时间】: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


【解决方案1】:

我想我找到了错误。我不确定,因为我手头没有足够权限的用户,因此无法对其进行测试。密码包含一个特殊字符,所以我在定义它时必须使用 @ 符号。

如果这不是修复,我会回来更新。

【讨论】:

    猜你喜欢
    • 2012-10-12
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多