c# - 如何以另一个用户(la/netonly)身份启动 Internet Explorer 进程?

标签 c# internet-explorer winapi pinvoke ntlm

我正在尝试以另一个用户身份从我们的 WPF 应用程序启动 Internet Explorer,以便当我们的用户访问(内部)网站时,他们会通过集成 Windows 身份验证进行静默身份验证。

我们不想以其他用户身份启动 iexplore.exe,因为当您首次在计算机上启动进程并首次尝试设置 IE7/8 时,会出现奇怪的部署/环境问题。不过,如果您有一个解决方案如何将每台计算机上的每个 IE 安装程序静音,我很想听听。

回到我本来想问的问题。我可以使用 runas 从命令提示符获得我想要的确切 IE 模拟行为*(感谢 https://serverfault.com/questions/70376/runas-domain-account-still-asks-for-password ):

c:\> runas /noprofile /netonly /user:MyDomain\MyUser iexplore.exe

*注意:由于多种原因,我无法将 runas 用于我们的 WPF 应用程序,但最终结果正是我想要的。

无论如何,我想要执行 runas /noprofile /netonly iexplore.exe 的 C# 等效代码。

我已经完成了一半,P/Invoke 位于 CreateProcessWithLogonW 。这就是我所拥有的:

uint LOGON_NETCREDENTIALS_ONLY = 2;
var lpStartupInfo = new CreateProcessWithLogonW_PInvoke.STARTUPINFO();
CreateProcessWithLogonW_PInvoke.PROCESS_INFORMATION processInformation;

CreateProcessWithLogonW_PInvoke.CreateProcessWithLogonW(
                userName,
                domain,
                pw,
                LOGON_NETCREDENTIALS_ONLY,
                null,
                commandLine,
                0,
                null,
                null,
                ref lpStartupInfo,
                out processInformation);

这成功启动了 Internet Explorer,但似乎根本没有模拟用户。我可以通过 runas 命令模拟用户,所以我 98% 确定身份验证失败不是 IE/zone/password/IIS 设置,这只是我在调用 CreateProcessWithLogonW() 时没有正确执行的操作.

我注意到的一件事是,runas /netonly 命令只有在我添加 /noprofile 开关时才有效,这让我很困惑。我不知道如何通过 C# 中的 P/Invoke 设置此开关的等效项。

感谢任何解决方案的帮助(解决“IE 在第一次启动时运行向导”,或找到我缺少的怪异 P/Invoke 设置)。

最佳答案

好吧,我已经非常非常接近了。神奇的修复是将 -noframemerging 添加到 iexplore.exe 调用中,这......老实说,我不确定它的作用,它使用短语“进程框架”,这很棒,也许意味着什么给你。

无论如何,这个问题似乎已经解决。

var arguments = "-noframemerging " + url;
var pathToIExploreExe = GetFullPathToIExploreExe();
var commandLine = string.Format("\"{0}\" {1}", pathToIExploreExe, arguments);

uint LOGON_NETCREDENTIALS_ONLY = 2;
var lpStartupInfo = new CreateProcessWithLogonW_PInvoke.STARTUPINFO();
CreateProcessWithLogonW_PInvoke.PROCESS_INFORMATION processInformation;

CreateProcessWithLogonW_PInvoke.CreateProcessWithLogonW(
            userName,
            domain,
            pw,
            LOGON_NETCREDENTIALS_ONLY,
            null,
            commandLine,
            0,
            null,
            null,
            ref lpStartupInfo,
            out processInformation);

关于c# - 如何以另一个用户(la/netonly)身份启动 Internet Explorer 进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12064530/

相关文章:

c# - 小数点无法通过 JSON 回发正确传递到 C#

c# - .net 应用程序的生命周期是什么

internet-explorer - CSS3 动画在 IE9 中不工作

winapi - 使用打开的句柄删除或重命名文件

windows - 在 Windows 上确定磁盘几何结构

C# 获取从 X 开始按名称排序的目录

c# - System.Object.GetType 如何抛出 NullRefernceException

css3 适用于 jfiddle 但不适用于 IE

html - IE6 和 8 中的 CSS3PIE 问题

c++ - 暂时删除互斥句柄