Error:
Access to the path '...' is denied.
Solution:
After struggling with this one for a few hours I found that the problem was passing the right combination of credentials to the network resource, i.e. impersonation AND network credentials, both of which require interop services. One or the other just didn't seem to work on the domain.
- Add read/write permission on the share (and in security) for the domain account you will be using to access the resource (this method assumes you are running ASP.NET under the default machine account and that it has not been granted access)
- Add the following code as class files to your project:
- Add the main code (and update to use your settings):
... using System.Net; // need this for NetworkCredential ... public ActionResult FileUpload(int id, FormCollection collection) { if (Request.Files.Count > 0) { // your configuration here... var uploadfolder = "\\networkpath\share"; var username = "username"; var password = "password"; var domain = "domain"; var file = Request.Files[0]; var pers = new Impersonation(); pers.Impersonate(username, password, domain); var credential = new NetworkCredential(username, password, domain); using (new NetworkConnection(destination, credential)) { var path = uploadfolder; // get file name, IE includes full path so cut it off var parts = file.FileName.Split('\\'); var fileName = parts[parts.Length - 1]; file.SaveAs(Path.Combine(path, fileName)); } pers.Undo(); } return View(); }