Skip to content

EF6 + Client Cert Auth (ProvideClientCertificatesCallback) #59

@syserr0r

Description

@syserr0r

I am new to EF6 and trying to implement the ProvideClientCertificatesCallback so as to use certificate based authentication.

I have (barely) managed to get EF6 to use my custom IDbConnectionFactory but it seems to require hard-cording the connection string (as the default is only called if the connection string isn't specified, and my attempts at overriding with DbConfiguration.Loaded seem not to work).

Even so, the callback never seems to be called.

public class clientCertFactory: IDbConnectionFactory
{
	internal string _clientCertPath = "";

	public DbConnection CreateConnection(string nameOrConnectionString)
	{
		var c = new NpgsqlConnection("SSL Mode=Require;Username=xxx;Host=yyy;Database=zzz");
		if (ConfigurationManager.AppSettings.Get("clientCert") != null)
		{
			_clientCertPath = ConfigurationManager.AppSettings.Get("clientCert");
			/*c.ProvideClientCertificatesCallback += new ProvideClientCertificatesCallback((X509CertificateCollection clientCerts) => {
				X509Certificate2 cert = new X509Certificate2(_clientCertPath);
				clientCerts.Add(cert);
			});*/

			c.ProvideClientCertificatesCallback += new ProvideClientCertificatesCallback(ProvideClientCertificates);
		}
		return c;
	}

	internal void ProvideClientCertificates(X509CertificateCollection clientCerts)
	{
		System.Console.WriteLine(String.Format("Attempting to apply cert: {0}", _clientCertPath));
		X509Certificate2 cert = new X509Certificate2(_clientCertPath);
		clientCerts.Add(cert);
	}
}

Is client cert auth supported by EF6.Npgsql and if so, what is the recommended way of implementing it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions