【问题标题】:SSL on Service Stack服务堆栈上的 SSL
【发布时间】:2012-11-19 18:32:54
【问题描述】:

Mono 上的服务堆栈是否支持 SSL?

我只能访问 mac,我在此处找到的说明要求您使用 windows 工具创建 pvk 文件: http://joshua.perina.com/geo/post/using-ssl-https-with-mono-httplistener

该站点作为 linux 守护进程托管,使用 upstart 脚本保持服务器正常运行。

【问题讨论】:

    标签: c# mono servicestack


    【解决方案1】:

    我最终在我的应用程序主机中编写了一些代码来启用对服务堆栈的 SSL 支持,该服务堆栈在幕后使用了 HttpListener。以下代码将为控制台应用程序的服务堆栈启用 SSL:

    public class AppHost : AppHostHttpListenerBase
    {
        public AppHost() : base("Service", typeof(AppHost).Assembly) 
        {
        }
    
        public override void Configure(Funq.Container container)
        {
            Plugins.Add(new RazorFormat());
        }
    
        static void AddP12 (string filename, string password, ushort port)
        {
            string dirname = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string path = Path.Combine(dirname, ".mono");
            path = Path.Combine(path, "httplistener");
            if (!Directory.Exists(path))
            {
                Console.WriteLine("Creating directory: " + path);
                Directory.CreateDirectory(path);
            }
            X509Certificate2 x509 = null;
            try {
                x509 = new X509Certificate2 (filename, password);
            } catch (Exception e) {
                Console.Error.WriteLine ("error loading certificate [{0}]", e.Message);
                return;
            }
    
            string target_cert = Path.Combine (path, String.Format ("{0}.cer", port));
            if (File.Exists(target_cert)) 
            {
                Console.Error.WriteLine ("error: there is already a certificate for that port.");
                return;
            }
            string target_pvk = Path.Combine (path, String.Format ("{0}.pvk", port));
            if (File.Exists(target_pvk)) {
                Console.Error.WriteLine ("error: there is already a certificate for that port.");
                return;
            }
    
            using (Stream cer = File.OpenWrite (target_cert)) 
            {
                byte[] raw = x509.RawData;
                cer.Write (raw, 0, raw.Length);
            }
    
            PrivateKey pvk = new PrivateKey();
            pvk.RSA = x509.PrivateKey as RSA;
            pvk.Save(target_pvk);           
        }
    
        public static void Main(string[] args)
        {
            string listeningOn = string.Empty;
            if (args.Length == 1)
                listeningOn = "http://*:" + args[0] + "/";
            else if (args.Length == 3)
            {
                listeningOn = "https://*:" + args[0] + "/";
                AddP12(args[1], args[2], Convert.ToUInt16(args[0]));
            }
            else
            {
                Console.WriteLine("Usage: [port] [p12 certificate] [p12 password]");
                return;
            }
            AppHost appHost = new AppHost();
            appHost.Init();
            appHost.Start(listeningOn);
            Console.WriteLine("Service Stack Server started at {0}, listening on {1}", DateTime.Now, listeningOn);
            while (true) System.Threading.Thread.Sleep(100);
        }
    }
    

    【讨论】:

    • 这看起来只是将证书从一个位置复制到另一个位置。这有什么帮助?想必也需要操作系统中的证书配置?
    猜你喜欢
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多