Skip to content

Commit d4871f1

Browse files
committed
feat(mssql): support (local) aliases and Data Source in connection strings
1 parent 85292e5 commit d4871f1

3 files changed

Lines changed: 34 additions & 20 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
<Authors>vkuttyp</Authors>
88
<PackageLicenseExpression>MIT</PackageLicenseExpression>
99
<RepositoryUrl>https://github.com/vkuttyp/CosmoSQLClient-Dotnet</RepositoryUrl>
10-
<Version>1.5.7</Version>
10+
<Version>1.5.8</Version>
1111
</PropertyGroup>
1212
</Project>

src/CosmoSQLClient.MsSql/MsSqlConnection.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ public sealed record MsSqlConfiguration
2424
/// </summary>
2525
public string? InstanceName { get; init; }
2626
public required string Database { get; init; }
27-
public required string Username { get; init; }
28-
public required string Password { get; init; }
27+
public string? Username { get; init; }
28+
public string? Password { get; init; }
2929
/// <summary>Optional Windows domain for NTLM auth.</summary>
3030
public string? Domain { get; init; }
31+
public bool IntegratedSecurity { get; init; } = false;
3132
public bool TrustServerCertificate { get; init; } = false;
3233
public bool Encrypt { get; init; } = true;
3334
public int ConnectTimeout { get; init; } = 30;
3435
public string AppName { get; init; } = "CosmoSQLClient";
3536

3637
/// <summary>
3738
/// Parse a SQL Server connection string.
38-
/// Supports: Server=host,port; Database=...; User Id=...; Password=...; Encrypt=...; TrustServerCertificate=...
39+
/// Supports: Server=host,port; Database=...; User Id=...; Password=...; Encrypt=...; TrustServerCertificate=...; Integrated Security=True
3940
/// </summary>
4041
public static MsSqlConfiguration Parse(string connectionString)
4142
{
@@ -54,8 +55,12 @@ public static MsSqlConfiguration Parse(string connectionString)
5455
return null;
5556
}
5657

57-
var server = Get("Server", "Data Source", "DataSource")
58+
var server = Get("Server", "Data Source", "DataSource", "Address", "Addr", "Network Address")
5859
?? throw new ArgumentException("Connection string missing 'Server'.");
60+
61+
// Handle (local) and . aliases
62+
if (server == "(local)" || server == ".") server = "localhost";
63+
5964
string host = server;
6065
int port = 1433;
6166
string? instanceName = null;
@@ -81,18 +86,22 @@ public static MsSqlConfiguration Parse(string connectionString)
8186
bool trustCert = string.Equals(Get("TrustServerCertificate"), "true", StringComparison.OrdinalIgnoreCase)
8287
|| string.Equals(Get("TrustServerCertificate"), "yes", StringComparison.OrdinalIgnoreCase);
8388

89+
bool integratedSecurity = string.Equals(Get("Integrated Security"), "true", StringComparison.OrdinalIgnoreCase)
90+
|| string.Equals(Get("Integrated Security"), "sspi", StringComparison.OrdinalIgnoreCase)
91+
|| string.Equals(Get("Trusted_Connection"), "true", StringComparison.OrdinalIgnoreCase)
92+
|| string.Equals(Get("Trusted_Connection"), "yes", StringComparison.OrdinalIgnoreCase);
93+
8494
return new MsSqlConfiguration
8595
{
8696
Host = host,
8797
Port = port,
8898
InstanceName = instanceName,
8999
Database = Get("Database", "Initial Catalog", "InitialCatalog")
90100
?? throw new ArgumentException("Connection string missing 'Database'."),
91-
Username = Get("User Id", "User ID", "UserId", "UID", "User")
92-
?? throw new ArgumentException("Connection string missing 'User Id'."),
93-
Password = Get("Password", "PWD", "PW")
94-
?? throw new ArgumentException("Connection string missing 'Password'."),
101+
Username = Get("User Id", "User ID", "UserId", "UID", "User"),
102+
Password = Get("Password", "PWD", "PW"),
95103
Domain = Get("Domain"),
104+
IntegratedSecurity = integratedSecurity,
96105
Encrypt = encrypt,
97106
TrustServerCertificate = trustCert,
98107
};
@@ -647,12 +656,17 @@ await WritePacketsAsync(TdsPacketType.PreLogin, TdsPreLogin.BuildRequest(wantEnc
647656
}
648657

649658
// ── Login7 ────────────────────────────────────────────────────────────
659+
if (_config.IntegratedSecurity && string.IsNullOrEmpty(_config.Username))
660+
{
661+
throw SqlException.Auth("Integrated Security (SSPI) is not yet supported by this driver. Please provide a User Id and Password.");
662+
}
663+
650664
var loginPayload = TdsLogin7.Build(new TdsLogin7.Options
651665
{
652666
Host = _config.Host,
653667
Database = _config.Database,
654-
Username = _config.Username,
655-
Password = _config.Password,
668+
Username = _config.Username ?? string.Empty,
669+
Password = _config.Password ?? string.Empty,
656670
Domain = _config.Domain,
657671
AppName = _config.AppName,
658672
});

src/CosmoSQLClient.MsSql/Tds/TdsLogin7.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ private enum TypeFlags : byte { SqlType = 0x00 }
2525

2626
public sealed class Options
2727
{
28-
public required string Host { get; init; }
29-
public required string Database { get; init; }
30-
public required string Username { get; init; }
31-
public required string Password { get; init; }
32-
public string? Domain { get; init; }
33-
public string AppName { get; init; } = "CosmoSQLClient";
34-
public string Language { get; init; } = "us_english";
28+
public required string Host { get; init; }
29+
public required string Database { get; init; }
30+
public string? Username { get; init; }
31+
public string? Password { get; init; }
32+
public string? Domain { get; init; }
33+
public string AppName { get; init; } = "CosmoSQLClient";
34+
public string Language { get; init; } = "us_english";
3535
}
3636

3737
/// <summary>Build Login7 payload bytes (without the TDS packet header).</summary>
@@ -68,8 +68,8 @@ public static byte[] Build(Options opts)
6868
var fields = new List<(string? name, byte[]? bytes)>
6969
{
7070
(opts.Host, null),
71-
(opts.Username, null),
72-
(null, ObfuscatePassword(opts.Password)),
71+
(opts.Username ?? string.Empty, null),
72+
(null, ObfuscatePassword(opts.Password ?? string.Empty)),
7373
(opts.AppName, null),
7474
(opts.Host, null), // ServerName
7575
(null, null), // Extension

0 commit comments

Comments
 (0)