Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2732,7 +2732,7 @@ private SqlFedAuthToken GetFedAuthToken(SqlFedAuthInfo fedAuthInfo)
SqlAuthenticationProvider? authProvider = SqlAuthenticationProviderManager.GetProvider(ConnectionOptions.Authentication);
if (authProvider == null && _accessTokenCallback == null)
{
throw SQL.CannotFindAuthProvider(ConnectionOptions.Authentication.ToString());
throw SQL.CannotFindAuthProvider(ConnectionOptions.Authentication);
}

// We will perform retries if the provider indicates an error that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,26 @@ internal static Exception UnsupportedAuthenticationByProvider(string authenticat
return ADP.NotSupported(StringsHelper.GetString(Strings.SQL_UnsupportedAuthenticationByProvider, type, authentication));
}

internal static Exception CannotFindAuthProvider(string authentication)
{
return ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFindAuthProvider, authentication));
internal static Exception CannotFindAuthProvider(SqlAuthenticationMethod authentication)
{
string authName = authentication.ToString();

return authentication switch
{
#pragma warning disable 0618
SqlAuthenticationMethod.ActiveDirectoryPassword or
#pragma warning restore 0618
SqlAuthenticationMethod.ActiveDirectoryIntegrated or
SqlAuthenticationMethod.ActiveDirectoryInteractive or
SqlAuthenticationMethod.ActiveDirectoryServicePrincipal or
SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow or
SqlAuthenticationMethod.ActiveDirectoryManagedIdentity or
SqlAuthenticationMethod.ActiveDirectoryMSI or
SqlAuthenticationMethod.ActiveDirectoryDefault or
SqlAuthenticationMethod.ActiveDirectoryWorkloadIdentity
=> ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFindActiveDirectoryAuthProvider, authName)),
_ => ADP.Argument(StringsHelper.GetString(Strings.SQL_CannotFindAuthProvider, authName)),
};
}

internal static Exception ParameterCannotBeEmpty(string paramName)
Expand Down
13 changes: 11 additions & 2 deletions src/Microsoft.Data.SqlClient/src/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Microsoft.Data.SqlClient/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Expand Down Expand Up @@ -2583,6 +2583,9 @@
<data name="SQL_CannotFindAuthProvider" xml:space="preserve">
<value>Cannot find an authentication provider for '{0}'.</value>
</data>
<data name="SQL_CannotFindActiveDirectoryAuthProvider" xml:space="preserve">
<value>Cannot find an authentication provider for '{0}'. Install the 'Microsoft.Data.SqlClient.Extensions.Azure' NuGet package (https://www.nuget.org/packages/Microsoft.Data.SqlClient.Extensions.Azure) to use Active Directory (Entra ID) authentication methods.</value>
</data>
<data name="SQL_CannotGetAuthProviderConfig" xml:space="preserve">
<value>Failed to read the config section for authentication providers.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Xunit;

namespace Microsoft.Data.SqlClient.UnitTests
{
public class SqlUtilTests
{
[Theory]
#pragma warning disable 0618
[InlineData(SqlAuthenticationMethod.ActiveDirectoryPassword)]
#pragma warning restore 0618
[InlineData(SqlAuthenticationMethod.ActiveDirectoryIntegrated)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryInteractive)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryServicePrincipal)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryManagedIdentity)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryMSI)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryDefault)]
[InlineData(SqlAuthenticationMethod.ActiveDirectoryWorkloadIdentity)]
public void CannotFindAuthProvider_ActiveDirectoryMethod_ContainsPackageGuidance(SqlAuthenticationMethod authMethod)
{
Exception exception = SQL.CannotFindAuthProvider(authMethod);

Assert.IsType<ArgumentException>(exception);
Assert.Contains(authMethod.ToString(), exception.Message);
Assert.Contains("Microsoft.Data.SqlClient.Extensions.Azure", exception.Message);
}

[Theory]
[InlineData(SqlAuthenticationMethod.NotSpecified)]
[InlineData(SqlAuthenticationMethod.SqlPassword)]
public void CannotFindAuthProvider_NonActiveDirectoryMethod_DoesNotContainPackageGuidance(SqlAuthenticationMethod authMethod)
{
Exception exception = SQL.CannotFindAuthProvider(authMethod);

Assert.IsType<ArgumentException>(exception);
Assert.Contains(authMethod.ToString(), exception.Message);
Assert.DoesNotContain("Microsoft.Data.SqlClient.Extensions.Azure", exception.Message);
}
}
}
Loading