A lightweight .NET library for parsing Credential-in-URL connection strings (e.g., scheme://user:password@host:port/database?options) into structured objects, and converting them into Npgsql (PostgreSQL) or MongoDB connection strings.
- Parse complex database URIs into structured
ConnectionParameters. - Support for multiple schemes (PostgreSQL, MongoDB, etc.).
- Convert parsed parameters to standard connection strings for Npgsql and MongoDB.
- Handles query parameters as a dictionary.
- Lightweight and targets
.NET Standard 2.0. - Utilises C# 14 Extension Types for a clean and intuitive API.
dotnet add package ciu-parserusing UriCredentialParser;
// Parse a connection URI
var details = CredentialsParser.Parse(
"postgres://admin:secret@localhost:5432/testdb?timeout=30&sslmode=require");
Console.WriteLine(details.HostName); // localhost
Console.WriteLine(details.UserName); // admin
Console.WriteLine(details.AdditionalQueryParameters["timeout"]); // 30using UriCredentialParser;
using UriCredentialParser.Enums;
var connString = details.ToNpgsqlConnectionString(
pooling: true,
sslMode: PostgresSSLMode.Prefer
);using UriCredentialParser;
var (databaseUrl, databaseName) = details.ToMongoConnectionSplit();
// databaseUrl: mongodb://admin:secret@localhost:5432?timeout=30&sslmode=require
// databaseName: testdbCredentialsParser.Parse expects a valid absolute URI.
| Part of URL | Property | Description |
|---|---|---|
scheme |
Scheme |
e.g., postgres, mongodb, redis |
user:pass |
UserName / Password |
Extracted from UserInfo |
host |
HostName |
Server address |
port |
Port |
Numeric port or null if omitted |
/path |
DatabasePath |
The path segment (usually database name) |
?query |
AdditionalQueryParameters |
A Dictionary<string, string> of options |
The ConnectionParameters record encapsulates the parsed data:
Scheme: The URI scheme.HostName: The server host.UserName/Password: Credentials (defaults to empty strings if missing).DatabasePath: The database name or path.Port: The port number (int?).AdditionalQueryParameters: A dictionary of all query parameters.
This library supersedes the narrower PostgresConnString.NET and mongo-url-parser packages with a single, shared parser and typed exports.
Contributions are welcome! Please feel free to submit a Pull Request.
To build and test the project:
cd src
dotnet build UriCredentialParser.slnx
dotnet testThis project is licensed under the MIT License - see the LICENSE file for details.