How to read config values from a custom DriverConnectionProvider with Fluent NHibernate?

137 views Asked by At

I'm currently implementing MSI for Sql Server in a project that uses NHibernate, to get this I created a custom DriverConnectionProvider. The thing is that I need to set access token now and to get the access token I need to read a client id from the config file. Is there a way to pass parameters into the custom DriverConnectionProvider?

This is what I have:

public class MsiConnectionProvider : DriverConnectionProvider
{
    public override DbConnection GetConnection(string connectionString)
    {
        var credential = new DefaultAzureCredential(
            new DefaultAzureCredentialOptions
            {
                ManagedIdentityClientId = "Hard Coded Client Id" //I want to set this using a config variable
            }); 

        var tokenReq = new TokenRequestContext(new[] { "Hard Coded Resource Id" }); //I want to set this using a config variable
        var accessToken = credential.GetToken(tokenReq);

        var conn = (SqlConnection)Driver.CreateConnection();
        try
        {
            conn.AccessToken = accessToken.Token;
            conn.ConnectionString = ConnectionString;
            conn.Open();

        }
        catch (Exception)
        {
            conn.Dispose();
            throw;
        }

        return conn;
    }
}

It is supposed that the custom DriverConnectionProvider is called automatically after being set up. So I don't find a way to pass those values. Any ideas?

Thanks in advance.

0

There are 0 answers