0

I have an ASP.Net application running in IIS which creates a file in a specified location using CsvWriter. If I use the full UNC path I get error that Access to the path is denied, however if I use Drive letter it works fine. What is puzzling me is that it does work with UNC path in development environment.

This code which creates the file

using (var writer = new StreamWriter(fileName))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
    {
    csv.WriteField("Foo");
    csv.WriteField("Bar");
    csv.NextRecord();
    }

Following advice various posts such as Access to the path is denied and IIS7 Permissions Overview - ApplicationPoolIdentity and MS Documentation: Application Pool Identities. I was initially convinced my problem was security related. This was supported by the fact that the application would run perfectly in development environment (IIS Express) which uses my user credentials but not under IIS which uses Application Pool Identity. I gave MyApplicationPoolIdentity full access to the specific directory to no avail.

However, I discovered that if I use the drive letter (C:\myDirectory\mySubDirectory\myFile.csv) as opposed to the UNC path (\\myServer\myDirectory\mySubDirectory\myFile.csv) in production the file is created perfectly. Even though I have the application working my concern is that I may have some configuration issue with IIS or my server which may bite me later. So my question is what would cause the create file/write to fail using UNC path and not using Drive letter?

I am using Windows Server 2016.

Thanks Tony

Tony
  • 89
  • 1
  • 12

1 Answers1

0

I think it is caused by permission issues. First, did you get any related error messages? Or you can try the following methods:

1.you can use Process Monitor to see which account is being used to access the share and what permission are required.

2.Check to ensure the account that IIS is running under has needed rights to the troublesome UNC.

samwu
  • 3,857
  • 3
  • 11
  • 25
  • I also think its permissions. I used the process Monitor and when using drive letter (C:\myDirectory...) process w3wp.exe is able to CreateFile under process IIS APPPOOL\MyAppPool. When I use UNC path (\\MyServer\myDirectory...) the same process with same user gets result ACCESS DENIED. Which tells me it can't be user permissions. – Tony Jan 08 '21 at 09:45
  • Since it is a permission issue, then you only need to give iis account UNC permission. what authentication method are you using? in Application pool identity you can set custom account and use domain user for respective account. Now It gives you the ability to write and read the files from UNC Path. – samwu Jan 11 '21 at 08:40
  • I am using IUSR for Anonymous Authentication, I have also tried using Application pool identity. I am hesitant to use a domain user for application pool identity as we have a tight IIS Management environment. Your comment pointed me to this thread [link](https://stackoverflow.com/questions/22240439/granting-write-permissions-to-a-networked-unc-folder-for-asp-net-under-iis-7-5-a) where the answer references a bug in iis 7 on server 2008, which again points away from Authentication. I am using 2016 so the doubt the hotfix relates to me. Thanks for advice – Tony Jan 12 '21 at 08:00