IE8的InternetExplorer COM对象忽略活动选项卡

时间:2011-09-07 16:18:09

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

这是在.net 3.5 winform上运行的代码的Web单点登录。只要ie8只有一个标签打开,代码就可以运行ie6或ie8。问题是,如果用户打开一个新选项卡(选项卡2,3等)并导航到一个网站(组织内部的Web表单),将执行以下代码,但即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();
    }

2 个答案:

答案 0 :(得分:4)

事实证明,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)

答案 1 :(得分:3)

Internet Explorer没有任何公共标签API(除了允许您将导航定位到新的前景或背景标签)。每个ActiveX控件或BHO都单独加载到单个选项卡实例中。尝试从ShellWindows集合中走下来不太可能正常工作,相反,您应该让您的插件与其托管站点联系(例如IObjectWithSite :: SetSite将传达此信息),这将允许您确定您的托管选项卡。