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
  • 299

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,810
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