支持https的Owin自主控制台应用程序(无web api,无SignalR)

min*_*d1n 13 c# ssl self-hosting asp.net-web-api owin

使用SslStream和socket,我从头开始开发了一个https Web服务器.我可以将证书应用于来自C#代码的流并处理请求.

但是,我并没有弄清楚如何用Owin做到这一点.有没有人知道如何将证书绑定到自托管控制台应用程序?

例:

// Bind the below certificate to Owin host
var certificate = new X509Certificate2("server.pfx", "password");
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅下面的现有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;
        }


    }

}
Run Code Online (Sandbox Code Playgroud)

Lex*_* Li 5

Owin 自托管应用程序只需要绑定到代码中的正确 URL,而证书映射应通过 Windows HTTP API 单独完成。

netsh http show sslcert可以显示现有的映射,Jexus Manager提供了 UI。