c# - IE 8 的 InternetExplorer COM 对象忽略事件选项卡

标签 c# internet-explorer com single-sign-on shdocvw

这是在 .net 3.5 winform 上运行的网络单点登录代码。只要 ie8 只打开一个选项卡,代码就可以在 ie6 或 ie8 上正常运行。问题是,如果用户打开一个新选项卡(选项卡 2、3 等)并导航到网站(组织内部的 Web 表单),将执行以下代码,但 ie COM 自动化对象将返回 HTMLDocument对于第一个选项卡(选项卡 1),即使选项卡 2 是事件选项卡。我无法在任何地方的 InternetExplorer 或 HTMLDocument 类中找到任何 IE 选项卡引用。实际上,在 IE COM 自动化文档中几乎没有任何与 IE 选项卡相关的文档。

AutoResetEvent ie2_NavigateCompleteAutoReset;

    /// <summary>
    /// Given the handle of an Internet Explorer instance, this method performs single sign on to
    /// several known web login forms.
    /// </summary>
    /// <param name="iEFramHandle"></param>
    private void WebFormSignOn(int iEFramHandle)
    {
        foreach (SHDocVw.InternetExplorer ie2 in new SHDocVw.ShellWindows())
        {
            if (ie2.HWND == iEFramHandle)
            {
                while (true)
                {
                    Thread.Sleep(100);
                    if (ie2.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                    {
                        try
                        {
                            mshtml.HTMLDocument doc = (mshtml.HTMLDocument)ie2.Document;
                            ie2.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(ie2_NavigateComplete2);
                            ie2_NavigateCompleteAutoReset = new AutoResetEvent(false);

                            /*Find the username element and enter the user's username*/
                            mshtml.HTMLInputElement userID = (mshtml.HTMLInputElement)doc.all.item("username", 0);
                            userID.value = Globals.Username;

                            /*Find the password element and enter the user's password*/
                            mshtml.HTMLInputElement pwd = (mshtml.HTMLInputElement)doc.all.item("password", 0);
                            pwd.value = Globals.GetAppName();

                            /*Find the submit element/button and click it*/
                            mshtml.HTMLInputElement btnsubmit = (mshtml.HTMLInputElement)doc.all.item("submit", 0);
                            btnsubmit.click();

                            /*Wait up to 5 seconds for the form submit to complete.
                             This is to prevent this method from being called multiple times
                             while waiting for the form submit and subsequent navigation from completing.*/
                            ie2_NavigateCompleteAutoReset.WaitOne(5000);
                            return;
                        }
                        catch (Exception err)
                        {
                            Logger.Log(err.ToString(), Logger.StatusFlag.Error, this.ToString(), "WebFormSignOn");
                            return;
                        }
                        finally
                        {
                            /*Remove the event handler*/
                            ie2.NavigateComplete2 -= ie2_NavigateComplete2;

                        }
                    }
                }
            }
        }
    }

    void ie2_NavigateComplete2(object pDisp, ref object URL)
    {
        ie2_NavigateCompleteAutoReset.Set();
    }

最佳答案

事实证明,IE 8 中的每个选项卡都有自己的进程和句柄。在原始代码中,我总是从第一个 IEFrame 获取句柄。我修改了代码(如下),现在它可以工作了。变化在于,代码不仅查找第一个 IEFrame 句柄,还查找与触发调用 WebFormsSignOut 的方法的 url 相匹配的 LocationURL。

private void WebFormSignOn(int iEFramHandle,string addressBarText)
{
    var shellWindows = new SHDocVw.ShellWindows();
    foreach (SHDocVw.InternetExplorer ie2 in shellWindows)
    {
        if (ie2.LocationURL==addressBarText)
        { //rest of the code (see orignal post)

关于c# - IE 8 的 InternetExplorer COM 对象忽略事件选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7337204/

相关文章:

java - com4j 与 jacob 从 Java 调用 COM 方法

C# 在类外部创建静态对象与在类内部创建静态对象有什么区别?

c# - 确定自定义 winforms 控件的设计时上下文

javascript - 占位符和 IE != BFF(占位符和 IE9 不起作用)

javascript - 添加 W3C 有效 CSS 时 Internet Explorer 7 卡住

css - 最小高度不能正常工作?

用于免注册激活的 COM 组件

c# - 优化 linq 查询以对行进行排序?

c# - 使用表达式时设置 T 的值

c++ - 如何在 Internet Explorer COM 对象中应用 css?