Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自代码(C)和连接的SQL Server Express用户的ASP.NET模拟_C#_Asp.net_Sql Server_Sql Server Express_Impersonation - Fatal编程技术网

C# 来自代码(C)和连接的SQL Server Express用户的ASP.NET模拟

C# 来自代码(C)和连接的SQL Server Express用户的ASP.NET模拟,c#,asp.net,sql-server,sql-server-express,impersonation,C#,Asp.net,Sql Server,Sql Server Express,Impersonation,从代码C模拟ASP.NET并使用SQL Server Express时遇到问题: 我能够成功模拟并连接到SQL Server,但是执行SELECT CURRENT_USER返回guest而不是TestUser_001 注意:我尝试过使用LOGON32\u LOGON\u NETWORK\u CLEARTEXT进行模拟,然后在调用LogonUser时使用LOGON32\u LOGON\u INTERACTIVE进行模拟-结果保持不变。我从中执行此模拟代码的帐户已作为操作系统集的一部分。在Visua

从代码C模拟ASP.NET并使用SQL Server Express时遇到问题:

我能够成功模拟并连接到SQL Server,但是执行SELECT CURRENT_USER返回guest而不是TestUser_001

注意:我尝试过使用LOGON32\u LOGON\u NETWORK\u CLEARTEXT进行模拟,然后在调用LogonUser时使用LOGON32\u LOGON\u INTERACTIVE进行模拟-结果保持不变。我从中执行此模拟代码的帐户已作为操作系统集的一部分。在Visual Studio 2013的IIS Express F5下测试。我从未以TestUser_001的身份交互登录并连接到SQL Server Express实例-在Security\Logins下没有MY_DOMAIN\TestUser_001-希望在使用代码时自动执行此操作

using (var impersonation = new ImpersonationHelper("TestUser_001", "MY_DOMAIN", "pass"))
{
    using (var conn = new SqlConnection(@"Data Source=MY_DOMAIN\SQLEXPRESS;Integrated Security=True;"))
    {
        conn.Open();

        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = "SELECT CURRENT_USER";
            var result = cmd.ExecuteScalar();
        }
    }
}
以下是基本的ImpersonationHelper实现:

public class ImpersonationHelper : IDisposable
{
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
    public const int LOGON32_PROVIDER_DEFAULT = 0;

    [DllImport("advapi32.dll")]
    public static extern int LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        int dwLogonType,
        int dwLogonProvider,
        ref IntPtr phToken
    );

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(
        IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken
    );

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    private readonly WindowsImpersonationContext impersonationContext_;

    public ImpersonationHelper(string username, string domain, string password)
    {
        var token = IntPtr.Zero;
        var token_duplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref token_duplicate) != 0)
                {
                    var temp_windows_identity = new WindowsIdentity(token_duplicate);
                    impersonationContext_ = temp_windows_identity.Impersonate();

                    if (impersonationContext_ != null)
                    {
                        CloseHandle(token);
                        CloseHandle(token_duplicate);
                        return;
                    }
                }
            }
        }

        if (token != IntPtr.Zero)
            CloseHandle(token);

        if (token_duplicate != IntPtr.Zero)
            CloseHandle(token_duplicate);

        throw new Exception(string.Format("Unable to impersonate as {0}\\{1}.", username, domain));
    }

    public void Dispose()
    {
        impersonationContext_.Undo();
    }
}

谢谢你和所有帮助回答这个问题的人。我想我会将此标记为答案,因为模拟工作正常,并且SQL Server报告了正确的用户。

什么是ImpersonationHelper?构造函数是否已执行模拟,或者您是否需要先调用某个方法?ImpersonationHelper已执行模拟。WindowsIdentity.GetCurrent.Name在连接到SQL Express之前,在ImpersonationHelper之后调用时返回My_DOMAIN\TestUser_001。选择SUSER_SNAME;返回?选择SUSER\u SNAME;返回MY_DOMAIN\TestUser_001,但是用户登录名未列在Security\Logins下。