我试图冒充另一个域上的用户,以查询该域。有关某些背景,请参阅从单向信任访问用户信息。
当我使用本地域用户时,我的模拟工作正常。当我指定目标域时,它也通过 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;
}
有任何想法吗?谢谢。