借助 SslStream 和套接字,我从头开发了一个 https 网络服务器。 我可以从 C# 代码向流应用证书并处理请求。
但是,我不知道如何与 Owin 一起做这件事。 有谁知道如何将证书绑定(bind)到自托管控制台应用程序?
示例:
// Bind the below certificate to Owin host
var certificate = new X509Certificate2("server.pfx", "password");
详情请引用下面已有的Owin主机代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
namespace Owin.Startup
{
class Program
{
static void Main(string[] args)
{
int port = 8888;
string url = $"http://localhost:{port}";
using (WebApp.Start<Startup>(url))
{
Console.WriteLine($"Hosted: {url}");
Console.ReadLine();
}
}
}
public class Startup
{
private IAppBuilder app;
public void Configuration(IAppBuilder app)
{
#if DEBUG
app.UseErrorPage();
#endif
app.Use(new Func<AppFunc, AppFunc>(next => (async env =>
{
Console.WriteLine("Begin Request");
foreach (var i in env.Keys)
{
Console.WriteLine($"{i}\t={(env[i] == null ? "null" : env[i].ToString())}\t#\t{(env[i] == null ? "null" : env[i].GetType().FullName)}");
}
if (next != null)
{
await next.Invoke(env);
}
else
{
Console.WriteLine("Process Complete");
}
Console.WriteLine("End Request");
})));
app.UseWelcomePage("/");
this.app = app;
}
}
}
最佳答案
Owin 自托管应用程序只需在代码中绑定(bind)到正确的 URL,而证书映射应通过 Windows HTTP API 单独完成。
netsh http show sslcert
可以显示现有映射,并且 Jexus Manager提供用户界面。
关于c# - 支持 https 的 Owin 自托管控制台应用程序(无 Web API,无 SignalR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797748/