【发布时间】:2012-07-04 01:51:25
【问题描述】:
我正在尝试冒充另一个域上的用户,以查询该域。有关背景信息,请参阅Accessing user info from a one way trust。
当我使用本地域用户时,我的模拟工作正常。当我指定目标域时,它也在 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;
}
有什么想法吗? 谢谢。
【问题讨论】:
-
你的意思是impersonationContext为空吗?我看不到代码是如何工作的,因为 impersonationContext 没有在正确的范围内声明。使用
GetLastError检查LogonUser返回的错误码
标签: active-directory impersonation