|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System; |
| 6 | +using System.Buffers; |
5 | 7 | using Xunit;
|
6 | 8 |
|
7 | 9 | namespace Microsoft.Data.SqlClient.ManualTesting.Tests
|
@@ -56,12 +58,63 @@ public static void IntegratedAuthenticationTest_ServerSPN()
|
56 | 58 | TryOpenConnectionWithIntegratedAuthentication(builder.ConnectionString);
|
57 | 59 | }
|
58 | 60 |
|
| 61 | + [ConditionalFact(nameof(IsIntegratedSecurityEnvironmentSet), nameof(AreConnectionStringsSetup))] |
| 62 | + public static void CustomSspiContextGeneratorTest() |
| 63 | + { |
| 64 | + SqlConnectionStringBuilder builder = new(DataTestUtility.TCPConnectionString); |
| 65 | + builder.IntegratedSecurity = true; |
| 66 | + Assert.True(DataTestUtility.ParseDataSource(builder.DataSource, out string hostname, out int port, out string instanceName)); |
| 67 | + // Build the SPN for the server we are connecting to |
| 68 | + builder.ServerSPN = $"MSSQLSvc/{DataTestUtility.GetMachineFQDN(hostname)}"; |
| 69 | + if (!string.IsNullOrWhiteSpace(instanceName)) |
| 70 | + { |
| 71 | + builder.ServerSPN += ":" + instanceName; |
| 72 | + } |
| 73 | + |
| 74 | + using SqlConnection conn = new(builder.ConnectionString) |
| 75 | + { |
| 76 | + SspiContextProvider = new TestSspiContextProvider(), |
| 77 | + }; |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + conn.Open(); |
| 82 | + |
| 83 | + Assert.Fail("Expected to use custom SSPI context provider"); |
| 84 | + } |
| 85 | + catch (SspiTestException sspi) |
| 86 | + { |
| 87 | + Assert.Equal(sspi.AuthParams.ServerName, builder.DataSource); |
| 88 | + Assert.Equal(sspi.AuthParams.DatabaseName, builder.InitialCatalog); |
| 89 | + Assert.Equal(sspi.AuthParams.UserId, builder.UserID); |
| 90 | + Assert.Equal(sspi.AuthParams.Password, builder.Password); |
| 91 | + } |
| 92 | + } |
| 93 | + |
59 | 94 | private static void TryOpenConnectionWithIntegratedAuthentication(string connectionString)
|
60 | 95 | {
|
61 | 96 | using (SqlConnection connection = new SqlConnection(connectionString))
|
62 | 97 | {
|
63 | 98 | connection.Open();
|
64 | 99 | }
|
65 | 100 | }
|
| 101 | + |
| 102 | + private sealed class TestSspiContextProvider : SspiContextProvider |
| 103 | + { |
| 104 | + protected override bool GenerateContext(ReadOnlySpan<byte> incomingBlob, IBufferWriter<byte> outgoingBlobWriter, SspiAuthenticationParameters authParams) |
| 105 | + { |
| 106 | + throw new SspiTestException(authParams); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private sealed class SspiTestException : Exception |
| 111 | + { |
| 112 | + public SspiTestException(SspiAuthenticationParameters authParams) |
| 113 | + { |
| 114 | + AuthParams = authParams; |
| 115 | + } |
| 116 | + |
| 117 | + public SspiAuthenticationParameters AuthParams { get; } |
| 118 | + } |
66 | 119 | }
|
67 | 120 | }
|
0 commit comments