You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 8.0发布至IIS后无法获取进程窗口信息

问题:IIS发布后无法获取进程的MainWindowTitle和MainWindowHandle

我需要获取具有特定标题(MainWindowTitle)的特定进程。在Visual Studio本地调试时一切正常,但将应用发布到本地IIS后,虽然能找到相同数量的进程,但MainWindowTitle和MainWindowHandle始终为空(不过ProcessName是有值的)。

测试代码

public List<Process> GetProcesses()
{
    List<Process> processes = [];

    var allProcesses = Process.GetProcesses();

    _logger.LogInformation($"All processes: {allProcesses.Count()}");

    foreach (var process in allProcesses)
    {
        _logger.LogInformation($"Process name: {process.ProcessName}. Title: {process.MainWindowTitle}");

        nint mainWindowHandle = process.MainWindowHandle;


        if (mainWindowHandle != nint.Zero)
        {
            processes.Add(process);
        }
    }

    _logger.LogInformation($"Processes: {processes.Count()}");

    return processes;
}

web.config配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments="\.\ProcessesWebApplication.dll" stdoutLogEnabled="true" stdoutLogFile="\.\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
    <system.web>
        <identity impersonate="true" />
    </system.web>
</configuration>

已尝试的解决方法

  • 尝试发布到不同位置(wwwroot及外部)
  • 创建新的应用程序池/站点,尝试多种身份(ApplicationPoolIdentity、LocalSystem)- 开启/关闭加载用户配置文件

这似乎是权限/安全相关问题,但我该如何解决?直接从命令行运行应用也完全正常:C:\inetpub\wwwroot\ProcessesWebAplication>dotnet ProcessesWebApplication.dll


编辑补充

我为应用程序池尝试了多种身份用户,并使用以下代码记录结果:

WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
_logger.LogInformation($"Identity: {principal.Identity}. Name: {identity.Name}. Owner: {identity.Owner}. AuthenticationType: {identity.AuthenticationType}. IsAnonymous: {identity.IsAnonymous}. IsAuthenticated: {identity.IsAuthenticated}. ");

不同身份的日志结果

  • LocalSystem身份:
    Identity: System.Security.Principal.WindowsIdentity. Name: NT AUTHORITY\SYSTEM. Owner: S-xxx. AuthenticationType: NTLM. IsAnonymous: False. IsAuthenticated: True.

  • ApplicationPoolIdentity身份:
    Identity: System.Security.Principal.WindowsIdentity. Name: IIS APPPOOL\ProcessesWebAplication. Owner: S-xxx. AuthenticationType: Negotiate. IsAnonymous: False. IsAuthenticated: True.

  • 自定义账户(与Visual Studio调试时完全相同,但仍无法返回完整进程信息):
    Identity: System.Security.Principal.WindowsIdentity. Name: xxx\xxx. Owner: S-xxx. AuthenticationType: CloudAP. IsAnonymous: False. IsAuthenticated: True.

ManagementObjectSearcher在发布到IIS后似乎可以工作,但无法返回我需要用于发送命令的MainWindowHandle。


内容的提问来源于stack exchange,提问作者burr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 16:29:50