简体   繁体   English

IIS 7.5应用程序池使用错误的%APPDATA%作为自定义用户身份

[英]IIS 7.5 application pool uses wrong %APPDATA% for custom user as identity

I want my MVC3 web application to access %APPDATA% (eg C:\\Users\\MyUsername\\AppData\\Roaming on Windows 7) because I store configuration files there. 我希望我的MVC3 Web应用程序访问%APPDATA%(例如,Windows 7上的C:\\Users\\MyUsername\\AppData\\Roaming ),因为我在那里存储配置文件。 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). 因此,我在IIS中创建了一个具有用户“MyUsername”标识的应用程序池,通过使用该帐户登录创建了该用户的配置文件,并打开了“加载用户配置文件”选项(默认情况下仍为true)。 Impersonation is turned off. 模拟被关闭。

Now I have the problem that %APPDATA% (in C#): 现在我遇到%APPDATA%(在C#中)的问题:

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

resolves to c:\\windows\\system32\\inetsrv instead of C:\\Users\\MyUsername\\AppData\\Roaming . 解析为c:\\windows\\system32\\inetsrv而不是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 . 更新:更确切地说,上面的C#代码返回一个空字符串,因此Path.GetFullPath(Path.Combine(appdataDir, "MyAppName"))当前路径添加到我的应用程序名称,从而生成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. 我知道我之前使用Windows Server 2008 R2上的相同Web应用程序完成了这项工作,现在我在Windows 7上使用相同的主要版本7.5的IIS来解决这个问题。
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. 我使用了与以前相同的过程:创建一个新用户,以该用户身份登录以创建配置文件和APPDATA目录,然后使用此标识添加应用程序池,最后将Web应用程序添加到此池中。

Any ideas? 有任何想法吗?

Open your %WINDIR%\\System32\\inetsrv\\config\\applicationHost.config and look for <applicationPoolDefaults> . 打开%WINDIR%\\System32\\inetsrv\\config\\applicationHost.config并查找<applicationPoolDefaults> Under <processModel> , make sure you don't have setProfileEnvironment="false" . <processModel> ,确保您没有setProfileEnvironment="false" If you do, set it to true. 如果这样做,请将其设置为true。

Application Pools - Your application Pool - Advanced settings ... 应用程序池 - 您的应用程序池 - 高级设置......

Process Model - Load user Profile set True. 流程模型 - 加载用户配置文件设置为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/ 取自https://blogs.msdn.microsoft.com/vijaysk/2009/03/08/iis-7-tip-3-you-can-now-load-the-user-profile-of-the-application-游泳池身份/

I experienced the same problem recently. 我最近遇到了同样的问题。 As mentioned by Amit, the problem is that the user profile isn't loaded. 如Amit所述,问题是未加载用户配置文件。 The setting is for all application pools, and is in the applicationHost.config (typically C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config). 该设置适用于所有应用程序池,位于applicationHost.config中(通常为C:\\ Windows \\ System32 \\ inetsrv \\ config \\ applicationHost.config)。 If you update the applicationPoolDefaults elements as follows, it will work; 如果您按如下所示更新applicationPoolDefaults元素,它将起作用;

<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. 我们已经尝试使用IIS 7.5,并毫无问题地将其用于生产。

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 或者如果你喜欢powershell

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

Hope this helps 希望这可以帮助

I am experiencing the same problem. 我遇到了同样的问题。 Have you by chance installed the Visual Studio 11 beta? 你偶然安装了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. 我最近做了,我注意到4.0兼容的.dll如何使用我们的代码。 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. 在比较GetFolderPath(和相关)的4.0和4.5的反编译源之后,存在差异。 Whether they are the source of the problem...I'm not sure yet. 他们是否是问题的根源......我还不确定。

Edit 2: Here are the relevant changes. 编辑2:以下是相关更改。 I'm working on trying both to see if I get different results. 我正在努力尝试看看我是否得到不同的结果。 [code removed] [代码已删除]

Edit 3: 编辑3:

I've now tried calling SHGetFolderPath directly, which is what the .NET Framework ends up doing, anyway. 我现在尝试直接调用SHGetFolderPath,无论如何,这是.NET Framework最终要做的事情。 It returns E_ACCESSDENIED (-2147024891 / 0x80070005). 它返回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: 编辑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. 由于您获得了一个空字符串,您可能希望切换代码以使用SHGetFolderPath,这样您就可以获得HResult并至少知道究竟发生了什么。

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( );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM