将文件复制到网络共享我没有访问 [英] Copying a file to a network share i dont have access to
问题描述
这是这个问题
我试图将文件从我的本地用户临时文件夹复制到远程文件共享。
我没有访问远程文件共享,所以我必须扮演这确实给用户。
I am trying to copy a file from my local users temp folder to a remote file share. I do not have access to the remote file share, so i have to impersonate a user which does.
现在,我可以成功读取的文件远程服务器并复制本地,但我不能写一个本地文件共享,因为它给我的错误:
Now, i can successfully read a file from the remote server and copy it locally, however i cannot write a local file to the share, since it gives me the error:
拒绝访问本地文件(如我是现在假冒其他用户)。
Access denied for the LOCAL file (as i am now impersonating another user).
如果你需要一些代码,我可以将它张贴。
If you need some code i can post it.
推荐答案
好不容易才找到了答案,
Managed to find the answer,
我只是必须创建一个的FileStream
来的地方文件之前冒充远程用户,然后传递的FileStream
来复制功能。
I simply had to create a FileStream
to the local file BEFORE impersonating the remote user, and then pass that FileStream
to the copy function.
编辑:
因此,这里是我的全部文件复制程序
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);
}
这篇关于将文件复制到网络共享我没有访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!