A library to parse credential-in-url and convert them to database connection urls
$ dotnet add package ciu-parserThis package replaces both PostgresConnString.NET and mongo-url-parser as they are very basic packages.
It aims to grant general .NET support for parsing and converting URLs in the form "scheme://user:password@host:port/database?connectionparameters" (also known as Credential-In-Url) and converting them to formats easily used by database service providers for .NET.
You can install the package from NuGet:
Install-Package ciu-parser
or via the .NET CLI:
dotnet add package ciu-parser
or for Paket:
paket add ciu-parser
To parse a URL:
using UriCredentialParser;
// ...
var details = CredentialsParser.Parse("postgres://someuser:somepassword@somehost:381/somedatabase");
The resulting ConnectionParameters object contains the following properties:
Scheme - Database server schemeHostName - Database server hostnamePort - Port on which to connectUserName - User with which to authenticate to the serverPassword - Corresponding passwordDatabasePath - Database name within the serverAdditionalQueryParameters - Additional database parameters provided as query optionsCurrently, this library allows for generating Npgsql-compatible connection strings with the following parameters:
pooling: type: boolean, default: truesslMode: type: PostgresSSLMode (enum), default: PrefertrustServerCertificate: type: boolean, default: trueusing UriCredentialParser;
using UriCredentialParser.Enums;
// ...
var details = CredentialsParser.Parse("postgres://someuser:somepassword@somehost:381/somedatabase");
var connString = details.ToNpgsqlConnectionString();
// Result: User ID=someuser;Password=somepassword;Server=somehost;Port=381;Database=somedatabase;Pooling=true;SSL Mode=Prefer;Trust Server Certificate=true
This library also allows for generating a MongoDB-compatible connection string alongside the extracted database name:
using UriCredentialParser;
// ...
var details = CredentialsParser.Parse("mongodb://user:password@host:port/database-name?otheroptions");
var (dbUrl, dbName) = details.ToMongoConnectionSplit();
// dbUrl: mongodb://user:password@host:port?otheroptions
// dbName: database-name
Feel free to make requests and to open pull requests with fixes and updates.