Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public bool SetProvider(SqlAuthenticationMethod authenticationMethod, SqlAuthent
if (candidateMethod == authenticationMethod)
{
_sqlAuthLogger.LogError(nameof(SqlAuthenticationProviderManager), methodName, $"Failed to add provider {GetProviderType(provider)} because a user-defined provider with type {GetProviderType(_providers[authenticationMethod])} already existed for authentication {authenticationMethod}.");
break;
return false; // return here to avoid replacing user-defined provider
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using System.Security;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Data.SqlClient.FunctionalTests.DataCommon;
using Xunit;

namespace Microsoft.Data.SqlClient.Tests
Expand Down Expand Up @@ -50,6 +50,18 @@ private void InvalidCombinationCheck(SqlCredential credential)
}
}

[Fact]
public async Task IsDummySqlAuthenticationProviderSetByDefault()
{
var provider = SqlAuthenticationProvider.GetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive);

Assert.NotNull(provider);
Assert.Equal(typeof(DummySqlAuthenticationProvider), provider.GetType());

var token = await provider.AcquireTokenAsync(null);
Assert.Equal(token.AccessToken, DummySqlAuthenticationProvider.DUMMY_TOKEN_STR);
}

[Fact]
public void CustomActiveDirectoryProviderTest()
{
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.Data.SqlClient/tests/FunctionalTests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="SqlClientAuthenticationProviders" type="Microsoft.Data.SqlClient.SqlClientAuthenticationProviderConfigurationSection,Microsoft.Data.SqlClient" />
</configSections>
<SqlClientAuthenticationProviders>
<providers>
<add name="active directory interactive" type="Microsoft.Data.SqlClient.FunctionalTests.DataCommon.DummySqlAuthenticationProvider, FunctionalTests" />
</providers>
</SqlClientAuthenticationProviders>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 System.Threading.Tasks;

namespace Microsoft.Data.SqlClient.FunctionalTests.DataCommon
{
/// <summary>
/// Dummy class to override default Sql Authentication provider in functional tests.
/// This type returns a dummy access token and is only used for registration test from app.config file.
/// Since no actual connections are intended to be made in Functional tests,
/// this type is added by default to validate config file registration scenario.
/// </summary>
public class DummySqlAuthenticationProvider : SqlAuthenticationProvider
{
public static string DUMMY_TOKEN_STR = "dummy_access_token";

public override Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters)
=> Task.FromResult(new SqlAuthenticationToken(DUMMY_TOKEN_STR, new DateTimeOffset(DateTime.Now.AddHours(2))));

// Supported authentication modes don't matter for dummy test, but added to demonstrate config file usage.
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod)
=> authenticationMethod == SqlAuthenticationMethod.ActiveDirectoryInteractive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<Compile Include="AlwaysEncryptedTests\Utility.cs" />
<Compile Include="AssertExtensions.cs" />
<Compile Include="DataCommon\AssemblyResourceManager.cs" />
<Compile Include="DataCommon\DummySqlAuthenticationProvider.cs" />
<Compile Include="DataCommon\SystemDataResourceManager.cs" />
<Compile Include="DataCommon\TestUtility.cs" />
<Compile Include="LocalizationTest.cs" />
Expand Down
Loading