Eternal Learner
Eternal Learner

Reputation: 121

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.

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. I know that when I archive the file at C:\Temp\Archive (my machine), the program works perfectly. I am also able to write a new file without any trouble to \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?

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! :(

Upvotes: 2

Views: 3182

Answers (1)

Eternal Learner
Eternal Learner

Reputation: 121

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!

Upvotes: 1

Related Questions