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



使用SslStream和socket,我从头开始开发了一个https web服务器。我可以从C#代码向流应用证书并处理请求。

然而,我不知道如何与Owin一起做到这一点。有人知道如何将证书绑定到自托管控制台应用程序吗?

示例:

// 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自托管应用程序只需要绑定到代码中正确的URL,而证书映射应该通过Windows HTTP API单独完成。

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

最新更新