绕过 WebView2 中显示的 Kestrel 服务器的无效 SSL 证书

Ano*_*ous 3 .net-core webview2

给定

  • WPF 应用程序启动 Kestrel 服务器
  • Kestrel 聆听http://0.0.0.0:5000https://0.0.0.0:6000
  • Kestrel 指向静态 HTML 文件index.html
  • WPF显示指向的浏览器控件WebView2https://127.0.0.1:6000/index.html

结果

  • 如果 WebView2 指向http://127.0.0.1:5000/index.html一切正常
  • 如果指向 WebView2,https://127.0.0.1:6000/index.html我会收到有关不受信任的证书的错误

问题

  • 是否可以在 Kestrel 或 WebView2 中禁用或忽略本地主机的 SSL 验证

不应触及 Windows 设置,例如将“localhost”证书标记为“msmc”中受信任的证书或生成自签名证书,因为此 WPF 应用程序应该在不同的计算机上运行。

换句话说,一定有比本文描述的更简单的方法

红隼

公共类Web服务器
{
  公共静态任务运行()
  {
    var 配置 = new ConfigurationBuilder().Build();

    var url = 新[]
    {
      “http://0.0.0.0:7000”,
      “https://0.0.0.0:8000”
    };

    var 环境 = WebHost
      .CreateDefaultBuilder(新字符串[0])
      .UseConfiguration(配置)
      .UseUrls(url)
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseIISIntegration()
      .UseStartup<WebStartup>();

    返回环境.Build().RunAsync();
  }
}

公共类 WebStartup
{
  公共 IConfiguration 配置 { 获取;}

  public WebStartup(IConfiguration配置)
  {
    配置=配置;
  }

  公共无效ConfigureServices(IServiceCollection服务)
  {
    services.AddSpaStaticFiles(配置=>
    {
      配置.RootPath =“index.html”;
    });
  }

  公共无效配置(IApplicationBuilder 应用程序,IWebHostEnvironment env)
  {
    app.UseDeveloperExceptionPage();
    //app.UseHsts();
    //app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseSpaStaticFiles();
  }
}

WPF 中的 WebView2 控件

公共主窗口()
{
  WebServer.Run();

  初始化组件();

  WebView.Source = new Uri("https://127.0.0.1:6000/index.html"); // HTTP 在 5000 上有效,HTTPS 6000 - 不行
  WebView.NavigationCompleted +=(对象发送者,CoreWebView2NavigationCompletedEventArgs args)=>
  {
    WebView.InvalidateVisual();
  };
}

Dav*_*ney 5

WebView2 目前不直接公开该功能。如果您愿意,可以在WebView2 反馈中提出问题,我们可以提出功能请求。

作为解决方法,您可以尝试使用CoreWebView2.CallDevToolsProtocolMethodAsync 方法来调用Security.setIgnoreCertificateErrors DevTools Protocol 方法。但是,我还没有尝试过 setIgnoreCertificateErrors ,并且它也标记为实验性的,因此不能肯定它会按照您想要的方式工作。

  • 我可以确认这是有效的(WebView2 1.0.818.41)并且这行代码 `var result = wait webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Security.setIgnoreCertificateErrors", "{\"ignore\": true}");` 结果是一个空的 json 对象,我将此代码放置在 `webView.CoreWebView2InitializationCompleted` 事件处理程序中。 (4认同)