在非同一域中的2个受保护源之间复制文件

本文关键字:受保护 之间 复制 文件 2个 | 更新日期: 2023-09-27 18:12:33

我有一个正在升级的小型部署工具。该工具从构建盒中获取代码版本,更新SVN,然后将其放到X服务器上(a部署将部署安装的特定部分移动到堆栈中的不同服务器上)。

现在的情况是,当它在构建盒以外的任何设备上运行时,由于安全问题,它将无法工作。

我们的构建盒是内部的,在我们自己的域中。我们要复制的服务器在一个高安全域。我使用了这里解释的技术:在c#中访问Windows中受密码保护的网络驱动器?为了访问这些域驱动器上的文件/数据,所以我不需要映射它。

但这里有个问题。

Build box - Domain A

部署服务器-域B部署服务器2 -域B

我的盒子完全控制我们的构建盒,因为开发人员以管理员的身份运行,它在我们的域中。然而,一旦我模拟了我的登录,所以我在域B上,我就不能访问我的域A构建盒。

这是一个内部实用程序,任何帮助将不胜感激。

*如果有大量的工作要做,而不是复制,我可以打开新的线程,并运行命令行从每个服务器上的SVN获取这些文件,因为这是一种可能性,而不是复制。我们将所有的部署安装文件保存在SVN中。

IntPtr token;
if (!Security.Access.LogonUser("ChuckNorris", "a_small_bunny[0]", "OfficeSpace", Security.Enums.LogonType.NewCredentials, Security.Enums.LogonProvider.Default, out token))
{
    throw new Win32Exception();
}
try
{
    IntPtr dToken;
    if (!Security.Access.DuplicateToken(token, Security.Enums.SecurityImpersonationLevel.Impersonation, out dToken))
        throw new Win32Exception();
    try
    {
        using (WindowsImpersonationContext iContext = new WindowsIdentity(dToken).Impersonate())
        {
            Directory.CreateDirectory(destDir); //Works Here as I have impersonation
            // copy each file to destination
     //This will bomb as my user is now linked to the prod domain.
            foreach (string file in Directory.GetFiles(srcDir))
            {
                // update property bag
                UpdatePropertyBag(
                    propertyBag,
                    PropertyBag.Step,
                    "Copying [" + file + "] to [" + destDir + "]");
                // copy each file
                File.Copy(file, CombinePath(destDir, Path.GetFileName(file)));
            }
            // deal with each file/folder
            foreach (string dir in Directory.GetDirectories(srcDir))
            {
                // copy each subdirectory
                CopyDirectory(propertyBag, srcDir, destDir, Path.GetFileName(dir));
            }
            iContext.Undo();
        }
    }
    catch (Exception ex)
    {
    }
    finally
    {
        if (dToken != IntPtr.Zero)
        {
            if (!Security.Access.CloseHandle(dToken))
            {
                // Uncomment if you need to know this case.
                ////throw new Win32Exception();
            }
        }
    }
}
catch (Exception ex)
{
}
finally
{
    if (token != IntPtr.Zero)
    {
        if (!Security.Access.CloseHandle(token))
        {
            // Uncomment if you need to know this case.
            ////throw new Win32Exception();
        }
    }
}

在非同一域中的2个受保护源之间复制文件

我可能在上面的流程中遗漏了一些东西,但是你可以:

  1. 模拟域A
  2. 复制到具有两个域权限的共享位置。
  3. 模拟域b,移动到最终位置。其他选项是读取文件详细信息,加载到内存中,并写入目标,必要时保留时间戳。