[英]Unauthorized Access When Moving a File, But the Account I'm Impersonating Has Permission?
On my MVC website, there is a page that archives an old file and writes a new one. 在我的MVC网站上,有一个页面可以存档一个旧文件并写入一个新文件。
However, when I archive the old file, I get the following error: 但是,当我存档旧文件时,出现以下错误:
System.UnauthorizedAccessException was caught
HResult=-2147024891
Message=Access to the path is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost)
at System.IO.File.Move(String sourceFileName, String destFileName)
at Controller.Action in C:\Program\Controllers\MyController.cs:line 201
InnerException:
I've checked the permissions on the account that I'm impersonating, and the account has Modify, Read & execute, List folder contents, Read, and Write permissions on the folder I'm trying to write to. 我已经检查了要模拟的帐户的权限,并且该帐户对我要写入的文件夹具有“修改”,“读取和执行”,“列出文件夹内容”,“读取”和“写入”权限。
Maybe I'm missing something about the file permissions? 也许我缺少有关文件权限的信息? The error is when I'm trying to move the file into the archive on \\server123\\D$\\Archive.
错误是当我尝试将文件移到\\ server123 \\ D $ \\ Archive上的存档中时。 I know that when I archive the file at C:\\Temp\\Archive (my machine), the program works perfectly.
我知道当我在C:\\ Temp \\ Archive(我的机器)上归档文件时,该程序可以正常运行。 I am also able to write a new file without any trouble to \\server123\\Test.
我也能够写一个新文件到\\ server123 \\ Test没有任何麻烦。 Could the error be because I'm moving from one drive of the server to another?
错误可能是因为我正在从服务器的一个驱动器移动到另一个驱动器吗? If that is the case... is there a way around it so I can write from \\server123\\Test to \\server123\\D$\\Archive?
如果是这样,是否有解决办法,这样我就可以从\\ server123 \\ Test写入\\ server123 \\ D $ \\ Archive?
Here is the code I'm using to move the file. 这是我用来移动文件的代码。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult MoveFile(MoveFileViewModel model)
{
string cartonsXml = GetCartonsXml(model);
try
{
//Impersonate user who has access to both folders.
string user = ConfigurationManager.AppSettings["FileUser"].ToString();
string domain = ConfigurationManager.AppSettings["FileDomain"].ToString();
string password = ConfigurationManager.AppSettings["FilePassword"].ToString();
ImpersonateUser impersonateUser = new ImpersonateUser();
IntPtr token = impersonateUser.GetUserToken(user, domain, password);
if (token != IntPtr.Zero)
{
using (WindowsImpersonationContext wic = WindowsIdentity.Impersonate(token))
{
//Move old cartons.xml file to archive.
string oldCartonsFilePath = Path.Combine(@"\\server123\Test", "cartons.xml");
string archiveFilePath = Path.Combine(@"\\server123\D$\Archive", "cartons("
+ DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Year + ")." + Guid.NewGuid().ToString() + ".xml");
System.IO.File.Move(oldCartonsFilePath, archiveFilePath); //This is where I catch the exception!
//Write new cartons.xml file.
string newCartonsFilePath = Path.Combine(@"\\server123\Test", "cartons.xml");
using (StreamWriter sw = new StreamWriter(newCartonsFilePath))
{
sw.WriteLine(cartonsXml);
sw.Close();
}
ViewBag.MsgText = "Complete!";
ViewBag.MsgColor = "Green";
}
}
else
{
ViewBag.MsgText = "Credentials failed! Files not moved!";
ViewBag.MsgColor = "Red";
}
}
catch (Exception ex)
{
ViewBag.MsgText = ex.Message;
ViewBag.MsgColor = "Red";
}
return View(model);
}
Please help! 请帮忙! :(
:(
After a variety of trials and errors, I created a share of the folder I'm trying to access, granted the account I'm using read/write permissions to it, and now the code works correctly. 经过各种试验和错误,我创建了我要访问的文件夹的共享,并授予了我对其使用读/写权限的帐户,现在代码可以正常工作。 I will be updating my code based on the comments I've seen here.
我将根据在这里看到的评论更新代码。 Thanks guys!
多谢你们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.