2011-09-07 23 views
5

これは.net 3.5 winformで実行されるWebシングルサインオンコードです。 ie8は1つのタブしか開いていない限り、ie6またはie8のコードは正常に実行されます。問題は、ユーザーが新しいタブ(タブ2,3など)を開き、Webサイト(組織内のWebフォーム)に移動すると、以下のコードが実行されますが、COMオートメーションオブジェクトがHTMLDocumentタブ2がアクティブなタブであっても、最初のタブ(タブ1)に対してInternetExplorerやHTMLDocumentクラスのIEタブの参照はどこにもありません。実際、IEのCOMオートメーションドキュメントには、IEタブ関連のドキュメントはほとんどありません。InternetExplorerでアクティブなタブが無視されるInternet Explorer 8用のCOMオブジェクト

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

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) 
3

Internet Explorerには、パブリックタブAPIはありません(新しいフォアグラウンドまたはバックグラウンドタブへのナビゲーションをターゲットにすることはできません)。各ActiveXコントロールまたはBHOは、個々のタブインスタンスに個別に読み込まれます。 ShellWindowsコレクションを下ろそうとするのは一般的ではありませんが、プラグインをホスティングサイトに手渡す必要があります(例:IObjectWithSite :: SetSiteがこの情報を伝えます)。

関連する問題