I've run into an issue that was originally exposed by Microsoft.CSharp.CSharpCodeGenerator
throwing an UnauthorizedAccessException: Access to c:\Users\[wrong-user]\AppData\Local\Temp
receiving an "access denied" error attempting to write into a different IIS AppPool user's temp directory.
I have another application which actually runs as the [wrong-user]
AppPool, and that application does work. However, this makes sense, since it has write access to it's own user directory.
I've narrowed the error down to the fact that %USERPROFILE%
is returning the path of a different IIS APPPOOL user, even though Environment.GetFolderPath(SpecialFolder.UserProfile)
returns the correct path.
If I enumerate Environment.GetEnvironmentVariables(User)
, I get:
- Path - C:\Users\[wrong-user]\AppData\Local\Microsoft\WindowsApps;
- TEMP - C:\Users\[wrong-user]\AppData\Local\Temp
- TMP - C:\Users\[wrong-user]\AppData\Local\Temp
Additionally, manually accessing the user's environment variables via HKEY_USERS/[SID]/Environment
confirms that it is simply down to %USERPROFILE%
being incorrect:
- Path - %USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
- TEMP - %USERPROFILE%\AppData\Local\Temp
- TMP - %USERPROFILE%\AppData\Local\Temp