title: ASP.NET is not authorized to access the requested resource error occurs
body: <p>I am trying to upload a file to a folder using ASP.NET MVC. The issues I am facing is an error:</p>
  
  <blockquote>
    <p>ASP.NET is not authorized to access the requested resource</p>
  </blockquote>
  
  <p>Here is a full description of error:</p>
  
  <blockquote>
    <p>Access to the path 'D:\test' is denied.</p>
    
    <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
    
    <p>Exception Details: <code>System.UnauthorizedAccessException</code>: Access to the path 'D:\test' is denied. </p>
    
    <p>ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. </p>
    
    <p>To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.</p>
  </blockquote>
  
  <p>I tried to give full permission to all types of accounts like network service, local user, admin, iis_iusers, etc. - basically almost all user groups/accounts. However, I didn't find any clues</p>
  
  <p>Below is the code I have used:</p>
  
  <p>HTML markup:</p>
  
  <pre><code>&lt;form action="~/home/uploadfiles" method="post" enctype="multipart/form-data"&gt;
      &lt;label for="file"&gt;Filename:&lt;/label&gt;
      &lt;input type="text" for="file" /&gt;
      &lt;input type="file" name="file" id="file" /&gt;
  
      &lt;input type="submit" name="submit" value="Submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>C# code:</p>
  
  <pre><code>public class ViewDataUploadFilesResult  //class of file
  {
              public string Name { get; set; }
              public int Length { get; set; }
  }
  
  public ActionResult UploadFiles()
  {
              var r = new List&lt;ViewDataUploadFilesResult&gt;();
  
              foreach (string file in Request.Files)
              {
                  HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                  if (hpf.ContentLength == 0)
                      continue;
                  var FilePath = @"D:\test";//Server.MapPath("/videos1");
                  string savedFileName = Path.Combine(
                     AppDomain.CurrentDomain.BaseDirectory,
                     Path.GetFileName(hpf.FileName));
                  hpf.SaveAs(FilePath);
  
                  r.Add(new ViewDataUploadFilesResult()
                  {
                      Name = savedFileName,
                      Length = hpf.ContentLength
                  });
              }
              //return View("UploadedFiles", r);
              return View("About");
  }
  </code></pre>