c# - 支持 https 的 Owin 自托管控制台应用程序(无 Web API,无 SignalR)

标签 c# ssl asp.net-web-api owin self-hosting

借助 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/

相关文章:

c# - 如何访问阻塞集合的底层默认并发队列

c# - Caliburn Micro 用户控制数据上下文

c# - 枚举类

c# - 调试时从数据行中获取所有列名/列值

ssl - 在登台服务器上将 Devise 与 SSL 结合使用时,如何修复重定向循环?

c# - 2 个项目上的 Webapi 2 Controller

http-post - 来自 fiddler 的 Web API POST 返回 400 错误请求,您的浏览器发送了无效请求

php - 诊断 "The connection to localhost was interrupted"

ssl - TLS/SSL 端点前的 TCP 负载平衡器

visual-studio - ASP.NET API 项目,Microsoft Identity Platform 配置出现错误 : Unrecognized command or argument '--code-update=true'