I am using Npgsql and I was wondering whether there was a way to connect to a PostgreSQL server using integrated security that automatically falls back to a public login? Or maybe there is another way to do what I am trying to achieve.
I have an ASP.NET web site (IIS7.5) that uses Windows Authentication, and I have configured PostgreSQL to allow access via SSPI, this works fine as long as there is a role with the right name in the database. The web site is available for everyone on the intranet to view but some users have a few extra abilities (or can see extra data) that everyone else should not be able to do.
Currently, I am using something like the following:
NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder();
csb.Pooling = false; // if true, causes intermittent DB connection issues
csb.Database = "dbname";
csb.Host = "192.168.1.100";
csb.IntegratedSecurity = true;
dbconn.ConnectionString = csb.ConnectionString;
try
{
dbconn.Connect();
}
catch (NpgsqlException ex)
{
if (/* check for authentication failed */)
{
csb.IntegratedSecurity = false;
csb.UserName = "www-read";
dbconn.ConnectionString = csb.ConnectionString;
dbconn.Open();
}
}
This works fine, except for each time it falls back to the "www-read" role, it adds a critical log entry saying that there was an authentication error. The only way I can see around this is always connecting via www-read and querying whether a suitable integrated-security role exists and reconnecting, but that seems even more cumbersome than the above.
I don't see any passwords in your code. Are you using any security at all?
If you don't want to embed password into your source code (and rightly so), you should create
~/.pgpass
file in your home directory (%appdata%/postgresql/pgpass.conf
on Windows) which has 1 or more lines like this:This will allow any program that is using libpq (including npgsql) to look up for passwords from .pgpass upon any connection attempt.
If this will run under www-data user, .pgpass should be created in its home directory (but be sure to configure your web server not to serve this file ever!)
I think doing this should also fix problem with connection pooling that you seem to have.