【问题标题】:Impersonate a user on another domain, one way trust模拟另一个域上的用户,一种方式信任
【发布时间】: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


【解决方案1】:

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

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

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多