要防止js文件下载并返回传输到C# WebBrowser,可以采取以下步骤:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Document != null)
{
webBrowser1.Document.ExecCommand("ClearAuthenticationCache", false, null);
webBrowser1.Document.ExecCommand("ClearCache", false, null);
webBrowser1.Document.ExecCommand("ClearHistory", false, null);
webBrowser1.Document.ExecCommand("ClearSelection", false, null);
webBrowser1.Document.ExecCommand("ClearType", false, null);
webBrowser1.Document.ExecCommand("ClearUndo", false, null);
webBrowser1.Document.ExecCommand("Delete", false, null);
webBrowser1.Document.ExecCommand("DeleteCookie", false, null);
webBrowser1.Document.ExecCommand("DeleteHistory", false, null);
webBrowser1.Document.ExecCommand("DeleteLocalStorage", false, null);
webBrowser1.Document.ExecCommand("DeleteSessionStorage", false, null);
webBrowser1.Document.ExecCommand("DeleteTemporaryFiles", false, null);
webBrowser1.Document.ExecCommand("DeleteUrlCache", false, null);
webBrowser1.Document.ExecCommand("DeleteUserData", false, null);
webBrowser1.Document.ExecCommand("DisableFullScreen", false, null);
webBrowser1.Document.ExecCommand("DisableImageResize", false, null);
webBrowser1.Document.ExecCommand("DisableTextResize", false, null);
webBrowser1.Document.ExecCommand("DisableVideoResize", false, null);
webBrowser1.Document.ExecCommand("EnableFullScreen", false, null);
webBrowser1.Document.ExecCommand("EnableImageResize", false, null);
webBrowser1.Document.ExecCommand("EnableTextResize", false, null);
webBrowser1.Document.ExecCommand("EnableVideoResize", false, null);
webBrowser1.Document.ExecCommand("ExitFullScreen", false, null);
webBrowser1.Document.ExecCommand("ExitPrintPreview", false, null);
webBrowser1.Document.ExecCommand("ExitTextResize", false, null);
webBrowser1.Document.ExecCommand("ExitVideoResize", false, null);
webBrowser1.Document.ExecCommand("Find", false, null);
webBrowser1.Document.ExecCommand("Forward", false, null);
webBrowser1.Document.ExecCommand("FullScreen", false, null);
webBrowser1.Document.ExecCommand("HideContextMenu", false, null);
webBrowser1.Document.ExecCommand("HideMediaControls", false, null);
webBrowser1.Document.ExecCommand("HidePrintButton", false, null);
webBrowser1.Document.ExecCommand("HideTextResize", false, null);
webBrowser1.Document.ExecCommand("HideVideoResize", false, null);
webBrowser1.Document.ExecCommand("Load", false, null);
webBrowser1.Document.ExecCommand("Print", false, null);
webBrowser1.Document.ExecCommand("PrintPreview", false, null);
webBrowser1.Document.ExecCommand("Refresh", false, null);
webBrowser1.Document.ExecCommand("SaveAs", false, null);
webBrowser1.Document.ExecCommand("SaveAsComplete", false, null);
webBrowser1.Document.ExecCommand("SaveAsFile", false, null);
webBrowser1.Document.ExecCommand("SaveAsImage", false, null);
webBrowser1.Document.ExecCommand("SaveAsWebArchive", false, null);
webBrowser1.Document.ExecCommand("SaveAsWord", false, null);
webBrowser1.Document.ExecCommand("SaveAsXml", false, null);
webBrowser1.Document.ExecCommand("SelectAll", false, null);
webBrowser1.Document.ExecCommand("SelectElement", false, null);
webBrowser1.Document.ExecCommand("SelectElementContents", false, null);
webBrowser1.Document.ExecCommand("SelectWord", false, null);
webBrowser1.Document.ExecCommand("ShowContextMenu", false, null);
webBrowser1.Document.ExecCommand("ShowMediaControls", false, null);
webBrowser1.Document.ExecCommand("ShowPrintButton", false, null);
webBrowser1.Document.ExecCommand("ShowTextResize", false, null);
webBrowser1.Document.ExecCommand("ShowVideoResize", false, null);
webBrowser1.Document.ExecCommand("Stop", false, null);
webBrowser1.Document.ExecCommand("Undo", false, null);
webBrowser1.Document.ExecCommand("Unselect", false, null);
webBrowser1.Document.ExecCommand("ViewSource", false, null);
}
}
需要注意的是,以上方法只是一些基本的防护措施,无法完全阻止有心人的攻击。为了更好地保护js文件的安全,还可以结合其他安全措施,如权限控制、访问控制、数据加密等。
领取专属 10元无门槛券
手把手带您无忧上云