locked
How to intercept a redirect in IWebBrowser2 control embedded in a win32 application RRS feed

  • Question

  • Hi,

    I am working on a win32 application that embeds an IWebBrowser2 control to load web pages. Initially, it will show a login page that will redirect (Http Status 302) multiple times to the correct identity provider. I would like to intercept these redirects on C++ side so that I can choose to stop/cancel a redirect if required.

    I was expecting that I would be able to intercept the redirects by implementing BeforeNavigate2 in DWebBrowserEvents2. But it is not being called for redirects. I am able to see the final URL in NavigateComplete but the UI already loads the webpage by that time.

    Is there an API for intercepting redirects before the control navigates to the final URL?

    Thanks
    Wednesday, May 13, 2020 3:59 AM

Answers

  • Extend your webbrowser site with IDocHostUIHandler (if you have no idea about this look at MFC's CHTMLView source code). In your IDocHostUIHandler::GetHostInfo implementation return a flag that has DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION set. By default WinInet does the redirection automatically and does not notify Urlmon. 


    Visual C++ MVP

    • Marked as answer by nangalvivek Saturday, May 16, 2020 3:17 PM
    Friday, May 15, 2020 8:49 PM

All replies

  • Hi,

    I am working on a win32 application that embeds an IWebBrowser2 control to load web pages. Initially, it will show a login page that will redirect (Http Status 302) multiple times to the correct identity provider. I would like to intercept these redirects on C++ side so that I can choose to stop/cancel a redirect if required.

    I was expecting that I would be able to intercept the redirects by implementing BeforeNavigate2 in DWebBrowserEvents2. But it is not being called for redirects. I am able to see the final URL in NavigateComplete but the UI already loads the webpage by that time.

    Is there an API for intercepting redirects before the control navigates to the final URL?

    Thanks
    Tuesday, May 12, 2020 5:52 PM
  • Hi,

    Since the interface IWebBrowser2 and DWebBrowserEvents2 are more related to the internet explorer development, you could post at the following forums to get better support. Thanks for understanding.

    Internet Explorer Development

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 13, 2020 1:49 AM
  • Hi nangalvivek,

    You said you want to intercept the redirect to stop or cancel the redirect.

    You can see in the documentation that there is a BeforeNavigate and BeforeNavigate2 event that occurs before the navigation. but these 2 events are not cancelable. So you cannot use these events for cancelling the navigation.

    I did not get any other event/ API that can intercept the navigation and cancel it.

    References:

    1. InternetExplorer object

    2. BeforeNavigate2 event

    Regards,

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 13, 2020 8:24 AM
  • What about the IBindCallbackRedirect interface?

    Wednesday, May 13, 2020 11:30 AM
  • Hi nangalvivek,

    I try to refer to the documentation for the IBindCallbackRedirect interface.

    It enables an application to control redirection behavior.

    Applications that implement the IBindCallbackRedirect interface can inspect and cancel redirection requests, including potentially malicious ones used in phishing attacks. For more info, see Overriding redirection requests.

    I suggest you can try to test it to check whether it has the similar behavior which you need in your application.

    Regards,

    Deepak



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 13, 2020 3:12 PM
  • Maybe you can check with NavigateError but the doc says it could not work...

    Wednesday, May 13, 2020 3:19 PM
  • Can you please share an example of how IBindCallbackRedirect needs to be implemented? I implemented the Redirect method in the interface but didn't receive any callback. I am not sure of the correct method to implement this interface with IWebBrowser2.
    Wednesday, May 13, 2020 7:31 PM
  • Hi nangalvivek,

    I will try to test the issue and try to provide you a sample once I finish the testing.

    Thanks for your understanding.

    Regards,

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, May 15, 2020 7:45 AM
  • Extend your webbrowser site with IDocHostUIHandler (if you have no idea about this look at MFC's CHTMLView source code). In your IDocHostUIHandler::GetHostInfo implementation return a flag that has DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION set. By default WinInet does the redirection automatically and does not notify Urlmon. 


    Visual C++ MVP

    • Marked as answer by nangalvivek Saturday, May 16, 2020 3:17 PM
    Friday, May 15, 2020 8:49 PM
  • Thank you so much! It worked!
    Saturday, May 16, 2020 3:17 PM