我如何在C#中以编程方式获取应用程序池的标识?我想要应用程序池用户,而不是当前登录的用户。
您可以使用 System.Security.Principal.WindowsIdentity.GetCurrent().Name
来识别当前应用程序正在运行的身份。 这个链接 提供了一个很好的实用工具,可以显示 aspx 运行的身份。
您需要引用Microsoft.Web.Administration(在Microsoft.Web.Administration.dll中)。 Microsoft.Web.Administration.dll位于C:\Windows\System32\inetsrv。
//Add this to your using statements:
using Microsoft.Web.Administration;
//You can get the App Pool identity like this:
public string GetAppPoolIdentity(string appPoolName)
{
var serverManager = new ServerManager();
ApplicationPool appPool = serverManager.ApplicationPools[appPoolName];
appPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
return appPool.ProcessModel.UserName;
}
另一种可能性是对我来说工作得相当好,而且不需要安装Microsoft.Web.Administration包及其众多依赖项:
string appPoolUserIdentity = WindowsIdentity.GetCurrent().Name;
From forums.asp.net
System.Security.Principal.WindowsIdentity.GetCurrent().Name
。 - Nate Anderson