5

これは、.net3.5WinFormで実行されるWebシングルサインオンコードです。ie8で開いているタブが1つしかない限り、コードはie6またはie8で正常に実行されます。問題は、ユーザーが新しいタブ(タブ2、3など)を開いてWebサイト(組織内のWebフォーム)に移動すると、以下のコードが実行されますが、COM自動化オブジェクトはHTMLDocumentを返すことです。タブ2がアクティブなタブである場合でも、最初のタブ(タブ1)の場合。InternetExplorerまたはHTMLDocumentクラスのどこにもIEタブの参照が見つかりません。実際、IECOM自動化ドキュメントのどこにも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();
    }
4

2 に答える 2

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)
于 2011-09-08T18:00:13.650 に答える
3

Internet ExplorerにはパブリックタブAPIがありません(ナビゲーションを新しいフォアグラウンドまたはバックグラウンドタブにターゲティングできるようにする以外に)。各ActiveXコントロールまたはBHOは、個別のタブインスタンスに個別にロードされます。ShellWindowsコレクションから離れようとすると、一般的には機能しない可能性があります。代わりに、プラグインをホスティングサイトにアクセスさせる必要があります(たとえば、IObjectWithSite :: SetSiteがこの情報を伝達します)。これにより、ホスティングタブを決定できます。

于 2011-09-08T02:52:54.863 に答える