同时模拟多个用户

impersonate multiple users at the same time

我正在 .net 中创建一项服务,将文件从一个域(域 A)复制到另一个域(域 B) 两个域都需要凭据才能连接到它们,所以我正在使用模拟。

using (new Impersonator(usr_name_source, domain_source, password_source))

模拟每次只对一个域有效,所以实际上我不知道如何同时模拟两个域以复制文件 所以我正在尝试这个:

using (new Impersonator(usr_name_source, domain_source, password_source))//server authentication
                                        {
                                            using (new Impersonator(usr_name_target, domain_target, password_target))//server authentication
                                            {
                                                DeleteOldFiles(targetPath);
                                                Copy(sourcePath, targetPath);
                                            }
                                        }

但它不像我模拟内部时那样工作new Impersonator(usr_name_target, domain_target, password_target)

它忘记了外部模拟。

有没有人知道如何在不映射驱动器等的情况下做到这一点...?

您一次只能模拟一个用户,因此您当前的解决方案不起作用。基本上,您正在尝试连接到两个不同的网络资源。您可以 P/Invoke WNetAddConnection2 函数连接到不同域的网络资源并根据需要执行操作。有关 WNetAddConnection2 的详细信息,请参见此处:https://msdn.microsoft.com/en-us/library/windows/desktop/aa385413(v=vs.85).aspx

通过这个 post 看看你如何 P/Invoke WNetAddConnection2: How to provide user name and password when connecting to a network share