【问题标题】:Owin self host console application with https support (no web api, no SignalR)支持 https 的 Owin 自托管控制台应用程序(无 Web api,无 SignalR)
【发布时间】:2016-02-21 05:35:23
【问题描述】:

使用 SslStream 和套接字,我从头开始开发了一个 https 网络服务器。 我可以从 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;
        }


    }

}

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 2012-09-03
      • 2013-05-07
      • 1970-01-01
      • 2013-07-09
      • 2019-02-24
      • 2016-01-21
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多