Reputation: 11
I'm attempting to write a web app that allows the user to upload a file to the server, which is then saved on a network share (ie X:\TargetFolder). First here's the code:
using (var fileStream = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
var inputStream = file.OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
}
The contents of the targetFile variable is:
"X:\\Intranet\\IN\\PartesTrabajo"
When this code runs in Kestrel or IIS Express, it works as expected, but when the site runs in IIS 7.5, I get the following exception:
2017-08-30 14:51:05.742 -04:00 [Error] An unhandled exception has occurred while executing the request
System.IO.DirectoryNotFoundException: Could not find a part of the path 'X:\Intranet\IN\PartesTrabajo\A1_7706_Copy of All Web Servers Batch 1.xlsx'.
at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Instrumentacion.Helpers.UtilityLibrary.<WriteFileInput>d__4.MoveNext() in C:\Users\kelly\Documents\Site Remediation\Remediated Sites\Barcelona\Instrumentacion\Helpers\FileHelpers.cs:line 42
This is obviously an IIS related permissions issue, but I can't figure out where the issue lies.
The app pool my .Net Core apps are running in (named .NetCore) are configured to run as ApplicationPoolIdentity. I've added explicit full control permissions to the IIS AppPool.NetCore user to this X: drive share. I've tried to switch the App Pool user to Network Service (and set explicit permissions for it as well), but got the same error. I've pointed this share at a local folder, yet keep getting this error, however if I point the save at the actual folder (ie C:\Users...\TargetFolder) then the writes work as expected.
I'm not sure where else to look!
Upvotes: 1
Views: 2326
Reputation: 2159
It works in Kestrel and IIS Express because the application runs in the context of your user session.
Network drives mapped in a user session are only reachable by that user, not by any service account which IIS is running under.
If you use the UNC path instead you will see that this works.
Upvotes: 1