简体   繁体   中英

Access to the path is denied in ASP.Net

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

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 useProcess 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM