0

I am trying to implement a solution into my application that mirrors the answer in this post

I have a similar scenario where I have an HttpListener and Grapevine based application running on an Ubuntu server that I need to get working with HTTPS using Mono and I am trying to create and include the relevant keys to allow HTTPS

The problem I am having is the last line of the solution,

key = PrivateKey.CreateFromFile (pvk_file).RSA;

When I try the same Visual Studio shows an error/text highlighted red, 'PrivateKey' does not have a definition for 'CreateFromFile'

Am I using the wrong libraries or is something else the issue with my code itself?

My code, cut down to the relevant method.

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using java.security;

public class ConfigureCertificates
    {
        private readonly string _dirName;
        private readonly string _path;
        private readonly string _port;
        private readonly string _certFile;
        public ConfigureCertificates(string port)
        {
            _dirName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            _path = Path.Combine(_dirName, ".mono");
            _path = Path.Combine(_path, "httplistener");
            _port = port;
            _certFile = Path.Combine(_path, String.Format("{0}.cer", _port));
        }

        public void SetUpCerts()
        {
            if (!File.Exists(_certFile))
               throw new Exception("Certificate file not found");

            string pvkFile = Path.Combine(_path, String.Format("{0}.pvk", _port));

            if (!File.Exists(pvkFile))
                throw new Exception("Private key not found");

            var cert = new X509Certificate2(_certFile);
            var key = PrivateKey.CreateFromFile(pvkFile).RSA; // Error occurs here
        }
    }
3therk1ll
  • 2,056
  • 4
  • 35
  • 64
  • 1
    Try the full namespace, perhaps there is a clash of class names here. `Mono.Security.Authenticode.PrivateKey.CreateFromFile()` – DavidG Aug 17 '16 at 23:07
  • That was spot on. Thanks. Just had to add `Mono.Security` from Nuget – 3therk1ll Aug 17 '16 at 23:11

1 Answers1

1

You have a naming clash - in other words there is another class called PrivateKey that doesn't have the method you require. A quick Google hunt indicates the correct class is in the Mono.Security.Authenticode namespace. So you will need to reference the full path:

Mono.Security.Authenticode.PrivateKey.CreateFromFile(...)

You may also need to add the Mono.Security package if you don't already have it.

DavidG
  • 113,891
  • 12
  • 217
  • 223