Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

owin

nancy

nowin

So I am using Nancy with Nowin.

The beauty of using Nowin is I don't have to mess around with various Windows commands to set up a simple web server. According to the Nowin readme I can configure SSL using the following line

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

However, when using Nancy I don't seem to have access to this Server builder class. Everything seems to happen magically behind the scenes.

Any ideas how I can pass the certificate through to Nowin?

like image 679
Mongus Pong Avatar asked Aug 24 '15 15:08

Mongus Pong


People also ask

How do I integrate SSL certificate into my website?

Under Install and Manage SSL for your site (HTTPS), click Manage SSL Sites. Scroll down to the Install an SSL Website and click Browse Certificates. Select the certificate that you want to activate and click Use Certificate. This will auto-fill the fields for the certificate.

How do I inspect my SSL certificate?

To check an SSL certificate on any website, all you need to do is follow two simple steps. First, check if the URL of the website begins with HTTPS, where S indicates it has an SSL certificate. Second, click on the padlock icon on the address bar to check all the detailed information related to the certificate.

How do I get https certificate for my server?

To obtain an HTTPS certificate, perform the following steps: Create a private and public key pair, and prepare a Certificate Signing Request (CSR), including information about the organization and the public key. Contact a certification authority and request an HTTPS certificate, based on the CSR.


2 Answers

  1. Make sure you have the Nancy.Owin package installed.

  2. Use code like this to start the server up:

.

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();
        }
    }
}
like image 56
Daryl Avatar answered Oct 24 '22 03:10

Daryl


If you look at this document, it says the following:

Configuration of OWIN

It'll just be there if the host sends it on.

If you use IIS as a host. You'll need to do the same 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 also does.

In the OWIN Demo, check this line:

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

Hope it helps you, good luck.

like image 41
Rafael Delboni Avatar answered Oct 24 '22 03:10

Rafael Delboni