给定
- WPF 应用程序启动 Kestrel 服务器
- 红隼倾听
http://0.0.0.0:5000并https://0.0.0.0:6000 - Kestrel 指向静态 HTML 文件
index.html - WPF 显示指向的浏览器控件 WebView2
https://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 中禁用或忽略 localhost 的 SSL 验证
不应触及 Windows 设置,例如在“msmc”中将“localhost”证书标记为受信任或生成自签名证书,因为该 WPF 应用程序应该在不同的计算机上运行。
换句话说,一定有比本文描述的更简单的方法。
红隼
公共类 WebServer
{
公共静态任务运行()
{
var configuration = new ConfigurationBuilder().Build();
变种网址 = 新 []
{
"http://0.0.0.0:7000",
“https://0.0.0.0:8000”
};
var 环境 = 网络主机
.CreateDefaultBuilder(新字符串[0])
.UseConfiguration(配置)
.UseUrls(网址)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<WebStartup>();
返回 environment.Build().RunAsync();
}
}
公共类 WebStartup
{
公共 IConfiguration 配置 { 获取;}
公共 WebStartup(IConfiguration 配置)
{
配置=配置;
}
公共无效配置服务(IServiceCollection 服务)
{
services.AddSpaStaticFiles(配置 =>
{
configuration.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 on 5000 有效,HTTPS 6000 - 否
WebView.NavigationCompleted += (对象发送者,CoreWebView2NavigationCompletedEventArgs args) =>
{
WebView.InvalidateVisual();
};
}