0

我试图冒充另一个域上的用户,以查询该域。有关某些背景,请参阅从单向信任访问用户信息。

当我使用本地域用户时,我的模拟工作正常。当我指定目标域时,它也通过 LDAPS 端口 636,它不起作用。我的模拟返回 null。

我的模拟代码

public static WindowsImpersonationContext ImpersonateUser(ConnectionCredentials user)
    {
        WindowsIdentity tempWindowsIdentity;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUser(user.UserName, user.Domain, user.Password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return impersonationContext;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
            CloseHandle(token);
        if (tokenDuplicate != IntPtr.Zero)
            CloseHandle(tokenDuplicate);
        return impersonationContext;
    }

有任何想法吗?谢谢。

4

1 回答 1

0

我的问题是我将用户名作为用户名@域发送,并指定了域名。如果用户名包含域名,LogonUser 的域名需要为空

if (LogonUser(user.UserName, null, user.Password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)

谢谢!

于 2012-07-03T17:52:37.427 回答