Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS ApplicationPool user has incorrect USERPROFILE / TEMP value

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
like image 611
Richard Szalay Avatar asked Mar 10 '26 15:03

Richard Szalay


1 Answers

The issue ended up being that setProfileEnvironment, which isolates the environment variables for each application pool's worker processes, was set to false on system.applicationHost/applicationPools/applicationPoolDefaults despite it having a default value of true according to the documentation. Since none of the application pools were overriding it, they all received the same value and thus shared common environment variables.

I have no idea how it got set to false, nor how the shared USERPROFILE environment variable got set to one application pool but never another, but setting the default value back to true resolved the issue.

It was this question/answer which helped me find the cause, despite the problem being different.

like image 61
Richard Szalay Avatar answered Mar 16 '26 03:03

Richard Szalay