IIS 7.5 application pool uses wrong %APPDATA% for custom user as identity
Asked Answered
C

5

15

I want my MVC3 web application to access %APPDATA% (e.g. C:\Users\MyUsername\AppData\Roaming on Windows 7) because I store configuration files there. Therefore I created an application pool in IIS with the identity of the user "MyUsername", created that user's profile by logging in with the account, and turned on the option "Load User Profile" (was true by default anyway). Impersonation is turned off.

Now I have the problem that %APPDATA% (in C#):

appdataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

resolves to c:\windows\system32\inetsrv instead of C:\Users\MyUsername\AppData\Roaming.

UPDATE: More exactly, the above C# code returns an empty string, so that Path.GetFullPath(Path.Combine(appdataDir, "MyAppName")) prepends the current path to my application name, resulting in c:\windows\system32\inetsrv\MyAppName.

I know I made this work before with the same web application on a Windows Server 2008 R2, and now I'm getting this problem with the same major version 7.5 of IIS on my Windows 7.
I used the same procedure as before: Created a new user, logged in as that user to create the profile and APPDATA directories, then added the application pool with this identity and finally added the web application to this pool.

Any ideas?

Cas answered 28/2, 2012 at 21:35 Comment(2)
Is your application pool configured as Classic or Integrated mode?Loreleilorelie
I have the same problem. What's especially weird is that the path for Environment.SpecialFolder.UserProfile works fine, and if I build up the path to the AppData folder from there, it works.Monosyllabic
C
24

Open your %WINDIR%\System32\inetsrv\config\applicationHost.config and look for <applicationPoolDefaults>. Under <processModel>, make sure you don't have setProfileEnvironment="false". If you do, set it to true.

Chieftain answered 10/12, 2012 at 17:59 Comment(5)
The value is <processModel identityType="ApplicationPoolIdentity" />.Cas
Set it to <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />Chieftain
This answer is the one that solved the problem for me.Lorrin
In my case, it was enough to add setProfileEnvironment="true" to the specific application pool. It was not necessary to change the setProfileEnvironment value in the default entry.Damara
Why is LoadUserProfile an option in IIS Manager, but SetProfileEnvironment is not?Dehydrate
I
5

Application Pools - Your application Pool - Advanced settings ...

Process Model - Load user Profile set True.

It Helps me.

Taken from https://blogs.msdn.microsoft.com/vijaysk/2009/03/08/iis-7-tip-3-you-can-now-load-the-user-profile-of-the-application-pool-identity/

Ible answered 11/12, 2017 at 14:11 Comment(0)
K
3

I experienced the same problem recently. As mentioned by Amit, the problem is that the user profile isn't loaded. The setting is for all application pools, and is in the applicationHost.config (typically C:\Windows\System32\inetsrv\config\applicationHost.config). If you update the applicationPoolDefaults elements as follows, it will work;

<applicationPoolDefaults managedRuntimeVersion="v4.0">
  <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
</applicationPoolDefaults>

We've tried this with IIS 7.5, and taken it through to production without problem.

You can automate this if you want;

appcmd set config -section:system.applicationHost/applicationPools /applicationPoolDefaults.processModel.setProfileEnvironment:"true" /commit:apphost

or if you prefer powershell

Set-WebConfigurationProperty "/system.applicationHost/applicationPools/applicationPoolDefaults/processModel" -PSPath IIS:\ -Name "setProfileEnvironment" -Value "true"

Hope this helps

Kalbli answered 12/4, 2017 at 12:28 Comment(0)
T
0

I am experiencing the same problem. Have you by chance installed the Visual Studio 11 beta? I did recently, and I've noticed a couple of differences in how the 4.0 compatible .dlls for that work with our code. I'm still trying to track down the problem for certain, but I didn't have this problem before that.

Edit:

After comparing the decompiled sources from 4.0 and 4.5 for GetFolderPath (and related), there are differences. Whether they are the source of the problem...I'm not sure yet.

Edit 2: Here are the relevant changes. I'm working on trying both to see if I get different results. [code removed]

Edit 3:

I've now tried calling SHGetFolderPath directly, which is what the .NET Framework ends up doing, anyway. It returns E_ACCESSDENIED (-2147024891 / 0x80070005). I don't know what has changed where I'm getting that in some specific cases, but not in others.

Edit 4:

Since you're getting a empty string, you may want to switch your code to use SHGetFolderPath so you can get the HResult and at least know what exactly is happening.

void Main() {
    Console.WriteLine( GetFolderPath( Environment.SpecialFolder.ApplicationData ) );
}

[System.Runtime.InteropServices.DllImport("shell32.dll")]
static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, uint dwFlags, StringBuilder pszPath);

private string GetFolderPath( Environment.SpecialFolder folder ) {
    var path = new StringBuilder( 260 );
    var hresult = SHGetFolderPath( IntPtr.Zero, (int) folder, IntPtr.Zero, 0, path );
    Console.WriteLine( hresult.ToString( "X" ) );

    return ( (object) path ).ToString( );
}
Tambourin answered 23/4, 2012 at 12:40 Comment(2)
No, I have Visual Studio 2010 installed, and no other betas. Which differences are there?Cas
I am also getting 0x80070005 (E_ACCESSDENIED). Will try to investigate more.Cas
K
0

The problem is with your IIS settings. The answer is here: Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) returns String.Empty

Kreda answered 19/7, 2012 at 13:45 Comment(4)
I have the user profile loaded and get the same problem (I think this problem occurs on Windows 7 only).Cas
Based on what you described in your original question, you did create the profile folders, but you didn't take the step necessary to load the profile for IIS. If you did take that step then add it to your description. If you didn't then read the link I gave and follow the instructions.Kreda
I said in my question that I had that option turned on, so this solution doesn't seem to work for me, sorry. Must have something to do with Windows 7. When I have time, I'll try on Windows 8 (not the server variant) and see what happens.Cas
I think you're right. I tried it with IIS on Win7 Pro and on Win7 Enterprise and GetFolderPath returns an empty string. Yet on Win2008 R2 it works fine. I also tried changing the app pool identity in Win7 to an account with higher priveliges and then got back "C:\Windows\system32\config\systemprofile\AppData\Roaming" which doesn't even exist. So my conclusion is the same as yours: it doesn't work on Win7. Good news is that it DOES work for IIS Express, so you can at least develop on your Win7 machine.Kreda

© 2022 - 2024 — McMap. All rights reserved.