Do not throw PNSE exception from NegotiateAuthentication constructor, report Unsuppor...
authorFilip Navara <filip.navara@gmail.com>
Thu, 14 Sep 2023 19:13:43 +0000 (23:13 +0400)
committerGitHub <noreply@github.com>
Thu, 14 Sep 2023 19:13:43 +0000 (12:13 -0700)
Co-authored-by: Carlos Sánchez López <1175054+carlossanlop@users.noreply.github.com>
src/libraries/System.Net.Http/tests/FunctionalTests/NtAuthTests.FakeServer.cs
src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Managed.cs
src/libraries/System.Net.Security/src/System/Net/NegotiateAuthenticationPal.Unix.cs

index 780db637ba335e289690a81a0b1de75ccc72b6c5..c29beca16f4015876a6460d1e0d328a9bf1888df 100644 (file)
@@ -139,5 +139,32 @@ namespace System.Net.Http.Functional.Tests
                     }).ConfigureAwait(false);
                 });
         }
+
+        [Fact]
+        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.Windows, "DefaultCredentials are unsupported for NTLM on Unix / Managed implementation")]
+        public async Task DefaultHandler_FakeServer_DefaultCredentials()
+        {
+            await LoopbackServer.CreateClientAndServerAsync(
+                async uri =>
+                {
+                    HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, uri);
+                    requestMessage.Version = new Version(1, 1);
+                    HttpMessageHandler handler = new HttpClientHandler() { Credentials = CredentialCache.DefaultCredentials };
+                    using (var client = new HttpClient(handler))
+                    {
+                        HttpResponseMessage response = await client.SendAsync(requestMessage);
+                        Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
+                    }
+                },
+                async server =>
+                {
+                    await server.AcceptConnectionAsync(async connection =>
+                    {
+                        var authHeader = "WWW-Authenticate: NTLM\r\n";
+                        await connection.SendResponseAsync(HttpStatusCode.Unauthorized, authHeader).ConfigureAwait(false);
+                        connection.CompleteRequestProcessing();
+                    }).ConfigureAwait(false);
+                });
+        }
     }
 }
index 4e5e8906b795ccfa1854484bdd67f2f999f22a8d..fff331646b73c608835e99b2f729497bd05e1288 100644 (file)
@@ -9,16 +9,23 @@ namespace System.Net
     {
         public static NegotiateAuthenticationPal Create(NegotiateAuthenticationClientOptions clientOptions)
         {
-            switch (clientOptions.Package)
+            try
             {
-                case NegotiationInfoClass.NTLM:
-                    return new ManagedNtlmNegotiateAuthenticationPal(clientOptions);
+                switch (clientOptions.Package)
+                {
+                    case NegotiationInfoClass.NTLM:
+                        return new ManagedNtlmNegotiateAuthenticationPal(clientOptions);
 
-                case NegotiationInfoClass.Negotiate:
-                    return new ManagedSpnegoNegotiateAuthenticationPal(clientOptions);
+                    case NegotiationInfoClass.Negotiate:
+                        return new ManagedSpnegoNegotiateAuthenticationPal(clientOptions);
 
-                default:
-                    return new UnsupportedNegotiateAuthenticationPal(clientOptions);
+                    default:
+                        return new UnsupportedNegotiateAuthenticationPal(clientOptions);
+                }
+            }
+            catch (PlatformNotSupportedException)
+            {
+                return new UnsupportedNegotiateAuthenticationPal(clientOptions);
             }
         }
 
index 900d66c05bfc7a85473b8e57ef5fcf10d7e26ada..ed1fe4e2e919374408f1e244b6fd3a507fd3e339 100644 (file)
@@ -23,20 +23,20 @@ namespace System.Net
 
         public static NegotiateAuthenticationPal Create(NegotiateAuthenticationClientOptions clientOptions)
         {
-            if (UseManagedNtlm)
+            try
             {
-                switch (clientOptions.Package)
+                if (UseManagedNtlm)
                 {
-                    case NegotiationInfoClass.NTLM:
-                        return new ManagedNtlmNegotiateAuthenticationPal(clientOptions);
+                    switch (clientOptions.Package)
+                    {
+                        case NegotiationInfoClass.NTLM:
+                            return new ManagedNtlmNegotiateAuthenticationPal(clientOptions);
 
-                    case NegotiationInfoClass.Negotiate:
-                        return new ManagedSpnegoNegotiateAuthenticationPal(clientOptions, supportKerberos: true);
+                        case NegotiationInfoClass.Negotiate:
+                            return new ManagedSpnegoNegotiateAuthenticationPal(clientOptions, supportKerberos: true);
+                    }
                 }
-            }
 
-            try
-            {
                 return new UnixNegotiateAuthenticationPal(clientOptions);
             }
             catch (Win32Exception)