14

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?

AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • Is your application pool configured as Classic or Integrated mode? – Kev Feb 29 '12 at 01:56
  • 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. – Jason Apr 20 '12 at 20:58

5 Answers5

22

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.

Amit Apple
  • 9,034
  • 41
  • 50
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/

Anton Semenov
  • 411
  • 5
  • 11
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

tapmantwo
  • 121
  • 2
  • 7
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( );
}
0

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

Community
  • 1
  • 1
grahamesd
  • 4,773
  • 1
  • 27
  • 27
  • I have the user profile loaded and get the same problem (I think this problem occurs on Windows 7 only). – AndiDog Jul 19 '12 at 14:36
  • 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. – grahamesd Jul 23 '12 at 17:16
  • 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. – AndiDog Jul 23 '12 at 20:02
  • 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. – grahamesd Aug 01 '12 at 20:52