【发布时间】:2012-03-26 11:09:38
【问题描述】:
我有网络文件共享。网络共享有登录帐户,当用户将文件复制到目录时,用户将被验证。首先,用户将在 c# 中使用WindowsImpersonationContext 在文件共享中创建目录。创建目录用户后正在将文件复制到网络共享中的目录。
用户能够在网络共享中创建目录,但是当用户通过将文件从本地计算机复制到网络文件共享来启动 robocopy 时,系统显示 Access denied。我在创建时使用相同的用户帐户来模拟文件共享帐户文件共享中的目录以及 robocopy。
请帮助我。请找到以下代码。 将文件复制到网络文件共享目录时,是否可以以其他帐户运行 robocopy 或冒充其他用户帐户。
在网络文件共享中创建目录
public void CreateFolder(string FilePath,string UserName,string DomainName,string Password)
{
try
{
string path = FilePath;
bool impersonateResult=impersonateValidUser(UserName, DomainName, Password);
if (impersonateResult)
{
Directory.CreateDirectory(path);
}
}
catch (Exception ex)
{
throw ex;
}
}
在 Filshare 中创建目录时的模拟
public bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
机械复制:
public void RoboCopy(string strSourceFilePath,string DestinationFilePath,string FileName,string UserName,String DomainName,string Password)
{
try
{
System.Security.SecureString password = new System.Security.SecureString();
char[] pass = Password.ToCharArray();
foreach (char c in pass)
password.AppendChar(c);
Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.FileName = "Robocopy.exe";
p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\" ", strSourceFilePath, DestinationFilePath, FileName,"/R:2" );
p.StartInfo.Domain = DomainName;
p.StartInfo.UserName = UserName;
p.StartInfo.Password = password;
p.StartInfo.UseShellExecute = false; // set this to false so that we can redirect the output
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string result = p.StandardOutput.ReadToEnd();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
catch (Exception ex)
{
// Setting the Error Description in CommonFunctions Class.
throw ex;
}
}
仅在执行 robocopy 时,当我使用 Fileshare 登录帐户将文件从本地计算机复制到 Network Filesahre 时,出现访问被拒绝错误。 我能够创建目录而不会出现任何拒绝访问错误吗?
【问题讨论】:
-
用户(你正在运行robocopy)是否有足够的权限来访问本地机器上的文件?
-
我已在我的本地电脑中添加了该用户帐户,并获得了完全权限。
-
你找到答案了吗?
标签: c#