c# - 将文件复制到我无权访问的网络共享

标签 c# security impersonation file-copying

这是 this 的扩展问题

我正在尝试将文件从本地用户的临时文件夹复制到远程文件共享。 我无权访问远程文件共享,所以我必须模拟一个有权限的用户。

现在,我可以成功地从远程服务器读取文件并将其复制到本地,但是我无法将本地文件写入共享,因为它给了我错误:

Access denied for the LOCAL file

(因为我现在正在冒充另一个用户)。

如果您需要一些代码,我可以发布。

最佳答案

设法找到答案,

我只需要在模拟远程用户之前为本地文件创建一个 FileStream,然后将该 FileStream 传递给复制函数。

编辑: 所以这是我的整个文件复制例程

using System.Security.Principal;
using System.Security.Permissions;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;

public class ImpersonatedFileCopy : IDisposable
{
    #region Assembly Functions
    [DllImport("advapi32.dll")]
    public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    [DllImport("kernel32.dll")]
    public static extern bool CloseHandle(IntPtr handle);
    #endregion

    #region Private Variables
    private IntPtr _TokenHandle = new IntPtr(0);
    private WindowsImpersonationContext _WindowsImpersonationContext;
    #endregion

    #region Constructors
    public ImpersonatedFileCopy(string domain, string username, string password)
    {
        Impersonate(domain, username, password);
    }
    #endregion

    #region Methods
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private void Impersonate(string domain, string username, string password)
    {
        bool returnValue;

        try
        {
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_LOGON_INTERACTIVE = 2;

            _TokenHandle = IntPtr.Zero;

            //Call LogonUser to obtain a handle to an access token.
            returnValue = LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref _TokenHandle);
            if (returnValue)
            {
                WindowsIdentity newId = new WindowsIdentity(_TokenHandle);
                _WindowsImpersonationContext = newId.Impersonate();
            }
        }
        catch (Exception ex)
        {
            UndoImpersonate();
            Debug.Writeline("Error"+ex.Message);
        }
    }

    private void UndoImpersonate()
    {
        if (_WindowsImpersonationContext != null)
        {
            _WindowsImpersonationContext.Undo();
            if (!_TokenHandle.Equals(IntPtr.Zero))
            {
                CloseHandle(_TokenHandle);
            }
        }
    }

    public bool PutFile(FileStream source, string destRemoteFilename, bool overwrite)
    {
        try
        {
            if (!Directory.Exists(Path.GetDirectoryName(destRemoteFilename))) Directory.CreateDirectory(Path.GetDirectoryName(destRemoteFilename));
            using (FileStream dest = File.OpenWrite(destRemoteFilename))
            {
               source.Seek(0, SeekOrigin.Begin);
               source.CopyTo(dest);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool GetFile(string sourceRemoteFilename, FileStream dest, bool overwrite)
    {
        try
        {
            using (FileStream source = File.OpenRead(sourceRemoteFilename))
            {
                source.Seek(0, SeekOrigin.Begin);
                source.CopyTo(dest);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    #endregion

    #region IDisposable
    public void Dispose()
    {
        UndoImpersonate();
        GC.SuppressFinalize(this);
    }
    #endregion
}

以及用法:

using (FileStream dest = File.OpenWrite(localDestinationFilename))
using (copy = new ImpersonatedFileCopy(domain,user,pass))
{
   success = copy.GetFile(remoteSourceFilename, dest, true);
}

关于c# - 将文件复制到我无权访问的网络共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425041/

相关文章:

c# - 如何使用反射对 Entity Framework 模型的属性执行 ToString

security - 来自安全组的 EC2 入站不起作用 - 我做错了什么?

c# - asp.net 模拟如何与 System.Threading.Task 一起工作?

c# - 实现 Entity Framework 6 添加或更新方法的最简单方法是什么

c# - 使其他类可以访问方法和控件

c# - 强制通用参数的类型是接口(interface)而不是特定类

java ssl错误无法支持TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384

ruby-on-rails - 我的开发机器上运行的 Rails 3.2.13 应用程序是否被黑客入侵?

c# - 如何使用不同的 Windows 帐户连接到 SQL Server

c# - 在模拟(窗口应用程序)时使用 Process.Start