6

I need to set the environment variable COMPLUS_FORCEENC=1 in an ASP.NET application. This variable must be set at the time the worker process starts to have an effect.

Therefore, I did this:

  1. Create a new user using the Computer Management console.
  2. Set a password.
  3. Make sure it is a member of Users and IIS_IUSRS.
  4. Configure the IIS Application Pool to use that user and load the user profile. This is using a custom pool, not the default one.
  5. Log in as that user to set the variable.
  6. Relogin to make sure the variable is set.
  7. Restart the OS.
  8. Make sure the application loads and works.

Unfortunately, the ASP.NET app does not see the variable. I printed the whole environment from within the app.

When I use Process Explorer on the worker process I see that it runs under the correct user but the variable is not set. Also, the TEMP variable points to C:\Windows\Temp and not (as expected) to the user profile's temp directory (which exists). I understand this is a sign of the user profile not being loaded.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\hivelist shows the hive of that user loaded. The Environment key for that user appears to be populated correctly:

enter image description here

What did I do wrong? How can I make the variable appear?

This is a Windows 7 machine.

boot4life
  • 269
  • 2
  • 5
  • 12

2 Answers2

6

Windows 7 SP1 introduced a new default setting for IIS that prevents the loading of profile specific environment variables for IIS application pools.

So, for Windows 7 SP1 you additionally need to edit the following file:

%windir%\system32\inetsrv\config\applicationHost.config

and add a processModel element with setProfileEnvironment set to true for your site app pool in the applicationPools section.

It should look like this:

<add name="ASP.NET v4.0" managedRuntimeVersion="v4.0">
      <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</add>

See Windows 7 SP1 causes IntelliTrace Collection to fail on IIS for more details.

Note: You should edit the applicationHost.config file with admin rights and after the change you should reboot the computer.

slayernoah
  • 1,570
  • 2
  • 12
  • 19
Sergey Vlasov
  • 161
  • 1
  • 3
0

Just set/create the environment variable within an asp.net page

like:

 System.Environment.SetEnvironmentvariable("myvar", "my value", System.EnvironmentvariableTarget.user)

this should survive AppPool recycles and even server reboots.

I have a full working example at this answer

Peter Hahndorf
  • 13,763
  • 3
  • 37
  • 58
  • I saw your post. I now tried setting the variable. This does not change anything. Using Procmon I see that the variable is set to exactly the location where it previously was set. After `iisreset` the variable still is not there. – boot4life Jul 13 '16 at 18:16
  • @boot4life - I just tested this under Windows 7, using the script in my linked answer. The environment variable survives an iisreset and a reboot. Are you sure you set `loadUserProfile` to true in the AppPool properties? I'm not using a normal user but `ApplicationPoolIdentity` and as the Process Model Identity. – Peter Hahndorf Jul 14 '16 at 05:59
  • I will try not using a normal user and report back. I'm sure loadProfile is set. I confirmed this by grepping apphost.config. – boot4life Jul 14 '16 at 11:05
  • I create a new pool, set it to AppPoolIdentity, LoadProfile is default-enabled. I made the app execute the code that you provided. The variable is still not set (according to the app and to procexp). What could cause IIS to silently fail to load user profiles? – boot4life Jul 15 '16 at 16:47
  • sorry I have no idea, why it doesn't work for you, it always worked for me. – Peter Hahndorf Jul 15 '16 at 17:52