I have an ASP.NET C# application running on IIS.
Some actions require me to read files which are put locally on the system, using Directory.EnumerateFiles
. This works when I call the function, because my windows user has access to those files, but it doesn't when another user calls the same page.
I checked with WindowsIdentity.GetCurrent().Name
and I indeed see CompanyName\MyName
as current user. The files contain sensitive data, and I prefer not to give read access to all users, even though they are on a remote system.
It seems to me that I want to give specifically my IIS application rights the read the folder, and somehow call the function Directory.EnumerateFiles from the IIS Application instead of from the current user performing the request (authorization is handled already in the application itself).
Is this feasible, and if so, how is it achieved?
Solution
I ended up using this answer: Can I turn off impersonation just in a couple instances:
using (WindowsIdentity.Impersonate(IntPtr.Zero))
{
//Directory.EnumerateFiles(...)
//File.ReadAllText(...)
}
Plus, I gave access to the folder for the following user: IIS APPPOOL\MyAppPoolName