使用 Nancy 时如何将 SSL 证书传递给 Nowin

How can I pass a SSL certificate to Nowin when using Nancy

所以我正在使用 Nancy with Nowin

使用 Nowin 的美妙之处在于我不必为了设置简单的 Web 服务器而使用各种 Windows 命令。根据 Nowin 自述文件,我可以使用以下行配置 SSL

builder.SetCertificate(new X509Certificate2("certificate.pfx", "password"));

但是,在使用 Nancy 时,我似乎无法访问此服务器构建器 class。一切似乎都在幕后神奇地发生。

知道如何将证书传递给 Nowin 吗?

我想你应该按照这篇文章中描述的方式进行操作:https://msdn.microsoft.com/en-us/magazine/dn451439.aspx 首先,您根据 Nowin 文档创建 Web 服务器,然后将 Nancy 添加为管道组件。我用 NowingSample(来自 Nowin 包)以这种方式进行了测试,它有效。

如果您查看 this document,它会显示以下内容:

OWIN 的配置

如果主机发送它就会在那里。

如果您使用 IIS 作为主机。您需要做同样的事情 config as with Aspnet. And you'll need an OWIN Aspnet host that supports the ClientCertificate. The one in the OWIN demo in Nancy does. The one by @prabirshrestha 也一样。

在 OWIN 演示中,勾选 this line:

if (request.ClientCertificate != null && request.ClientCertificate.Certificate.Length != 0)
        {
            env[OwinConstants.ClientCertificate] = new X509Certificate(request.ClientCertificate.Certificate);
        }

希望对你有帮助,祝你好运。

  1. 确保安装了 Nancy.Owin 软件包。

  2. 使用如下代码启动服务器:

.

using System;
using System.Net;
using System.Threading.Tasks;
using Nancy.Owin;
using Nowin;

public class Program
{
    static void Main(string[] args)
    {
        var myNancyAppFunc = NancyMiddleware.UseNancy()(NancyOptions options =>
        {
            // Modify Nancy options if desired;

            return Task.FromResult(0);
        });

        using (var server = ServerBuilder.New()
            .SetOwinApp(myNancyAppFunc)
            .SetEndPoint(new IPEndPoint(IPAddress.Any, 8080))
            .SetCertificate(new X509Certificate2("certificate.pfx", "password"))
            .Build()
        )
        {
            server.Start();

            Console.WriteLine("Running on 8080");
            Console.ReadLine();
        }
    }
}