首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何正确使用LogonUser从工作组客户端模拟域用户

如何正确使用LogonUser从工作组客户端模拟域用户
EN

Stack Overflow用户
提问于 2011-02-16 23:48:03
回答 7查看 113.7K关注 0票数 48

ASP.NET:模拟VMWare上的域

这个问题是我要问的,但答案并没有详细说明_token是如何派生出来的。它似乎只使用WindowsIdentity.GetCurrent().Token,因此不会发生模拟。

我可以在.NET中不同的Active域中模拟用户吗?

下一个问题有相互矛盾的答案,被接受的一个问题有一个评论:“我开始怀疑我的问题在别处。”没什么用。

LogonUser只适用于我的域

下一个问题似乎意味着它是不可能的,但它涉及两个领域,所以我不确定它是否相关。

我真正的问题是:

  • 有可能吗?,如果有,
  • 怎么做?还是我哪里出错了?

到目前为止,我尝试的是使用来自http://msdn.microsoft.com/en-us/library/chf6fbt4%28v=VS.80%29.aspx的代码

代码语言:javascript
运行
复制
bool returnValue = LogonUser(user, domain, password,
            LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
            ref tokenHandle);
// after this point, returnValue = false

Win32错误是

登录失败:用户名未知或密码错误

EN

回答 7

Stack Overflow用户

发布于 2012-03-12 15:19:34

很少有帖子建议使用LOGON_TYPE_NEW_CREDENTIALS而不是LOGON_TYPE_NETWORKLOGON_TYPE_INTERACTIVE。我有一个模拟问题,一个机器连接到一个域,而另一个没有,这修复了它。这个职位中的最后一个代码片段表明,在林中模拟确实有效,但它并没有具体说明建立信任的问题。所以这可能是值得一试的:

代码语言:javascript
运行
复制
const int LOGON_TYPE_NEW_CREDENTIALS = 9;
const int LOGON32_PROVIDER_WINNT50 = 3;
bool returnValue = LogonUser(user, domain, password,
            LOGON_TYPE_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50,
            ref tokenHandle);

MSDN说LOGON_TYPE_NEW_CREDENTIALS只在使用LOGON32_PROVIDER_WINNT50时才能工作。

票数 63
EN

Stack Overflow用户

发布于 2013-06-20 15:02:54

对我来说,这是一个完整的例子(我希望更多的人这样做):

代码语言:javascript
运行
复制
//logon impersonation
using System.Runtime.InteropServices; // DllImport
using System.Security.Principal; // WindowsImpersonationContext
using System.Security.Permissions; // PermissionSetAttribute

...

class Program {

    // obtains user token
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
        int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    // closes open handes returned by LogonUser
    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public extern static bool CloseHandle(IntPtr handle);

    public void DoWorkUnderImpersonation() {
        //elevate privileges before doing file copy to handle domain security
        WindowsImpersonationContext impersonationContext = null;
        IntPtr userHandle = IntPtr.Zero;
        const int LOGON32_PROVIDER_DEFAULT = 0;
        const int LOGON32_LOGON_INTERACTIVE = 2;
        string domain = ConfigurationManager.AppSettings["ImpersonationDomain"];
        string user = ConfigurationManager.AppSettings["ImpersonationUser"];
        string password = ConfigurationManager.AppSettings["ImpersonationPassword"];

        try {
            Console.WriteLine("windows identify before impersonation: " + WindowsIdentity.GetCurrent().Name);

            // if domain name was blank, assume local machine
            if (domain == "")
                domain = System.Environment.MachineName;

            // Call LogonUser to get a token for the user
            bool loggedOn = LogonUser(user,
                                        domain,
                                        password,
                                        LOGON32_LOGON_INTERACTIVE,
                                        LOGON32_PROVIDER_DEFAULT,
                                        ref userHandle);

            if (!loggedOn) {
                Console.WriteLine("Exception impersonating user, error code: " + Marshal.GetLastWin32Error());
                return;
            }

            // Begin impersonating the user
            impersonationContext = WindowsIdentity.Impersonate(userHandle);

            Console.WriteLine("Main() windows identify after impersonation: " + WindowsIdentity.GetCurrent().Name);

            //run the program with elevated privileges (like file copying from a domain server)
            DoWork();

        } catch (Exception ex) {
            Console.WriteLine("Exception impersonating user: " + ex.Message);
        } finally {
            // Clean up
            if (impersonationContext != null) {
                impersonationContext.Undo();
            }

            if (userHandle != IntPtr.Zero) {
                CloseHandle(userHandle);
            }
        }
    }


    private void DoWork() {
        //everything in here has elevated privileges

        //example access files on a network share through e$ 
        string[] files = System.IO.Directory.GetFiles(@"\\domainserver\e$\images", "*.jpg");
    }
}
票数 26
EN

Stack Overflow用户

发布于 2011-10-07 20:28:55

我也有同样的问题。不知道你是否解决了这个问题,但我真正想要做的是用AD凭证访问一个网络共享。在这种情况下,需要使用WNetAddConnection2()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5023607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档