Rich C Rich C - 2 months ago 5x
C# Question

'PrivateKey' does not have a definition for 'CreateFromFile'

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

based application running on an Ubuntu server that I need to get working with
and I am trying to create and include the relevant keys to allow

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;

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


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:


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