【问题标题】:Disabling Of Web Security When Using WebView2 in C# Coding Environment在 C# 编码环境中使用 WebView2 时禁用 Web 安全
【发布时间】:2021-06-08 07:10:51
【问题描述】:

在 Visual Studio C# 中使用 CEFSharp 作为 Embedded WebBrowser 时,您需要根据用户要求 禁用 Web 安全检查 作为解决方案这段代码:

  var cefSettings = new CefSettings();
  cefSettings.CefCommandLineArgs.Add("disable-web-security", "disable-web-security");
  Cef.Initialize(cefSettings);

Microsoft WebView2 是一个新产品,我现在需要在相同的 C# 编码环境中使用它时执行相同的禁用网络安全。我已经搜索了很多,但没有可能的解决方案。

有谁知道如何使用 WebView2 设置?

提前谢谢..

【问题讨论】:

  • 您需要禁用哪些特定的安全设置?
  • 感谢 Poul,网站必须连接一个 JNLP 套接字以使用 USB 阅读器对用户进行签名验证,EDGE 连接到同一个套接字时不会发出警告,但 WebView 会发出警告...

标签: c# webview2


【解决方案1】:

对于 WebView2,您可以使用 CoreWebView2EnvironmentOptions.AdditionalBrowserArguments 为浏览器进程设置命令行参数。这些是 Edge 浏览器接受的相同命令行参数,主要匹配 chromium command line switches,包括 --disable-web-security。

如果您使用的是 WPF 或 WinForms WebView2 控件,它将类似于以下内容:

CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions("--disable-web-security");
CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(null, null, options);
// EnsureCoreWebView2Async must be called before any other call
// to EnsureCoreWebView2Async and before setting the Source property
// since these will both cause initialization of the CoreWebView2 property
// but using a default CoreWebView2Environment rather than your custom one.
await webview2.EnsureCoreWebView2Async(environment);

当然请注意,如果不将 WebView2 中呈现的内容限制为您信任的内容,则不应使用禁用 Web 安全性。禁用 Web 安全并在 WebView2 中呈现未知内容是危险的。

【讨论】:

  • 选项 "--disable-web-security" 不起作用,但选项 "--allow-insecure-localhost" 成功了(得到这个选项来自链接“铬命令行开关”),非常感谢@David
  • 假设我们必须在 CoreWebView2EnvironmentOptions 中传递两个参数,那么正确的方法是什么?我想使用(--disable-web-security)和(--autoplay-policy=no-user-gesture-required)
【解决方案2】:

Windows 窗体中的代码:

public Form1()
{
   InitializeComponent();
   _ = InitializeAsync();
}
private async Task InitializeAsync()
{
    CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions("--allow-insecure-localhost");//--disable-web-security
    CoreWebView2Environment environment = await CoreWebView2Environment.CreateAsync(null, null, options);
    await webView21.EnsureCoreWebView2Async(environment);
    webView21.CoreWebView2.Navigate("https://yourlink.jnlp");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多