'UnauthorizedAccessException when accessing a Windows file share from .NET Core app
I have an application written in .NET 5 ( soon to be .NET 6 ) that allows users to access files stored on a bunch of different user defined Windows network shares. The application is secured through a BEARER token and is running in Linux using Kestrel.
Through this application a user is able to "attach" a file. Basicly, tell the application where an existing file is stored on a windows network share. Then other users can click on that link and the application will download the file for them.
Regardless of the username and password passed in, ( or if we provide the optional domain name ) the application always throws an UnauthorizedAccessException This is the current code and throws when calling GetRequestStreamAsync();
The format of the file to download is a typical UNC path.
\serverName\folder1\subfolder2\fileToDownload.pdf
which is converted to
file://serverName/folder1/subfolder2/fileToDownload.pdf
FileWebRequest request = (FileWebRequest)WebRequest.Create(uncLink);
request.Credentials = new NetworkCredential(userName, password);
request.Method = "POST";
Stream readStream = await request.GetRequestStreamAsync();
Every article I read is centered on IIS and is from ~10 years back so has been unhelpful.
Solution 1:[1]
To answer the question for anyone who may run across this in the future.
We found this article and based our code on that. Ultimately, this involved created a folder, that we then mounted the network share into, then we could treat that folder as if it were local and download the files from there.
Since we are running in docker we also needed to enable some capabilities.
"DockerfileRunArguments": "--cap-add=SYS_ADMIN --cap-add=DAC_READ_SEARCH"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Russ |
