如何在.NET WebBrowser控件中阻止下载? [英] How to block downloads in .NET WebBrowser control?

查看:186
本文介绍了如何在.NET WebBrowser控件中阻止下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要防止.NET WebBrowser控件显示任何您要打开还是保存此文件?和另存为对话框。相反,我想显示一个消息框,告知用户文件下载因为安全原因被禁用。

I need to prevent the .NET WebBrowser control from showing any "Do you want to open or save this file?" and "Save As" dialogs. Instead, I want to display a message box telling users that file downloads are disabled for security reasons.

我从$ code> FileDownload 事件 WebBrowser ,但不允许取消。然后,我使用了 CodeProject:Extended .NET 2.0 WebBrowser控件根据原始COM调用使用接口 DWebBrowserEvents2 来实现我自己的事件。当我根据一个MS知识库条目关于FileDownload签名的错误,事件处理程序被调用,我能够取消下载。

I started with the FileDownload event of WebBrowser, but it does not allow cancellation. Then, I used the approach from CodeProject: Extended .NET 2.0 WebBrowser Control to implement my own event based on the original COM call using the interface DWebBrowserEvents2. When I fixed the code according to an MS knowledge base entry about a bug with the FileDownload signature, the event handler was called and I was able to cancel the download.

这不适用于所有下载,虽然:下载URL指向一个URL,包括 .exe 引发事件,可以在对话框出现之前取消,但对于其他人(例如)。 do ),直到用户点击打开保存或者取消在对话框中。

This does not work with all downloads, though: download URLs pointing to an URL including .exe raise the event and can be cancelled before the dialog appears - but for others (like .do), the event handler is not called until the user clicks Open, Save or Cancel in the dialog.

可能的解决方案可能是截取 WH_CALLWNDPROCRET 消息和回答该对话框,然后向用户显示< a>,但它听起来很努力,我也宁愿更清洁的解决方案...

A possible solution might be to intercept WH_CALLWNDPROCRET messages and 'answer' the dialog before it is shown to the user, but it sounds like much effort and I also would prefer a cleaner solution...

有谁知道如何可靠地阻止所有下载?

Does anybody know how to reliably block all downloads?

推荐答案

唯一可靠的方法似乎是钩入Windows事件队列并禁止对话框(因为各种事情可以让用户访问)。这是我们的帮手班:

The only reliable way seems to be to hook into the Windows event queue and suppress the dialog boxes (as all sorts of things can get the user access). This is what our helper class does:

    void ListenForDialogCreation()
    {
        // Listen for name change changes across all processes/threads on current desktop...
        _WinEventHook = WinAPI.SetWinEventHook(WinAPI.EVENT_OBJECT_CREATE, procDelegate);
    }
    void StopListeningForDialogCreation()
    {
        WinAPI.UnhookWinEvent(_WinEventHook);
    }

    void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        const uint OBJID_WINDOW = 0;
        const uint CHILDID_SELF = 0;

        // filter out non-HWND, and things not children of the current application
        if (idObject != OBJID_WINDOW || idChild != CHILDID_SELF)
            return;

        //Get the window class name
        StringBuilder ClassName = new StringBuilder(100);
        WinAPI.GetClassName(hwnd, ClassName, ClassName.Capacity);

        // Send close message to any dialog
        if (ClassName.ToString() == "#32770")
        {
            WinAPI.SendMessage(hwnd, WinAPI.WM.CLOSE, IntPtr.Zero, IntPtr.Zero);
            if (OnDialogCancelled != null)
                OnDialogCancelled();
        }
        if (ClassName.ToString() == "#32768")
        {
            WinAPI.SendMessage(hwnd, WinAPI.WM.CLOSE, IntPtr.Zero, IntPtr.Zero);
            if (OnDialogCancelled != null)
                OnDialogCancelled();
        }

    }

    public delegate void OnDialogCancelledEvent();
    public event OnDialogCancelledEvent OnDialogCancelled;




  • #32770是Dialog课程

  • #32768是弹出菜单

  • WinAPI命名空间是我们的pinvoke包装器。

  • 如果您不希望阻止所有对话框,则在您吸引课程后,您需要添加一些其他过滤器。这取决于你需要多么安全。在$ WORK我们需要阻止所有上传和下载。

    If you don't want to block all Dialogs you'll want to add in some additional filters once you've caught the class. It depends how secure you need to be. At $WORK we needed to block all uploads and downloads.

    抑制弹出菜单是必需的,因为它可以访问帮助应用程序,该应用程序提供了指向微软网站的链接,该功能可以启动IE的完整实例。然后他们可以做任何他们想要的。

    Suppressing the pop-up menu is necessary as it gives access to the Help application, which gives links to microsoft's website, which enables a full instance of IE to be launched. Then they can do whatever they want.

    这篇关于如何在.NET WebBrowser控件中阻止下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆