Files
dotnet-runtime/src/libraries/System.Net.Http/tests/FunctionalTests/DefaultCredentialsTest.cs
T
David Shulman db9be5c4c1 Fix WinHttpHandler default cred auth when server offers multiple schemes
When a server offers multiple auth schemes, WinHttpHandler is supposed
to select the more secure scheme regardless of the order of the
authentication headers. So, despite a server offering Basic and
Negotitate (in that order), Negotiate would be selected.

When using default credentials, however, it was not calling
WinHttpSetCredentials(null, null). This resulted in the first scheme
offered by the server (Basic) to be attempted. But there were no
credentials set and so a 401 resulted.

Fix WinHttpHandler so that it will always call WinHttpSetCredentials
even with default credentials. This ensures that the selected auth
scheme (in this case Negotiate) is used correctly.

Revised test configuration instructions. An IHttpModule is used for the
test server endpoint to ensure that IIS will put 'Basic' first in the
response headers.


Commit migrated from https://github.com/dotnet/corefx/commit/9c6821d9d39ebe67b03f6e491489ffbb44844612
2016-05-15 17:00:02 -07:00

274 lines
11 KiB
C#

// 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.Net.Test.Common;
using System.Security.Principal;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.Http.Functional.Tests
{
// TODO: #2383 - Consolidate the use of the environment variable settings to Common/tests.
public class DefaultCredentialsTest
{
private static string DomainJoinedTestServer => TestSettings.Http.DomainJoinedHttpHost;
private static bool DomainJoinedTestsEnabled => !string.IsNullOrEmpty(DomainJoinedTestServer);
private static string SpecificUserName = "test";
private static string SpecificPassword = "Password1";
private static string SpecificDomain = DomainJoinedTestServer;
private static Uri AuthenticatedServer =
new Uri($"http://{DomainJoinedTestServer}/test/auth/negotiate/showidentity.ashx");
// This test endpoint offers multiple schemes, Basic and NTLM, in that specific order. This endpoint
// helps test that the client will use the stronger of the server proposed auth schemes and
// not the first auth scheme.
private static Uri MultipleSchemesAuthenticatedServer =
new Uri($"http://{DomainJoinedTestServer}/test/auth/multipleschemes/showidentity.ashx");
private readonly ITestOutputHelper _output;
private readonly NetworkCredential _specificCredential =
new NetworkCredential(SpecificUserName, SpecificPassword, SpecificDomain);
public DefaultCredentialsTest(ITestOutputHelper output)
{
_output = output;
_output.WriteLine(AuthenticatedServer.ToString());
}
[ConditionalTheory(nameof(DomainJoinedTestsEnabled))]
[InlineData(false)]
[InlineData(true)]
public async Task UseDefaultCredentials_DefaultValue_Unauthorized(bool useProxy)
{
var handler = new HttpClientHandler();
handler.UseProxy = useProxy;
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(AuthenticatedServer))
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}
}
[ConditionalTheory(nameof(DomainJoinedTestsEnabled))]
[InlineData(false)]
[InlineData(true)]
public async Task UseDefaultCredentials_SetFalse_Unauthorized(bool useProxy)
{
var handler = new HttpClientHandler { UseProxy = useProxy, UseDefaultCredentials = false };
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(AuthenticatedServer))
{
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
}
}
[ConditionalTheory(nameof(DomainJoinedTestsEnabled))]
[InlineData(false)]
[InlineData(true)]
public async Task UseDefaultCredentials_SetTrue_ConnectAsCurrentIdentity(bool useProxy)
{
var handler = new HttpClientHandler { UseProxy = useProxy, UseDefaultCredentials = true };
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(AuthenticatedServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseBody = await response.Content.ReadAsStringAsync();
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
_output.WriteLine("currentIdentity={0}", currentIdentity.Name);
VerifyAuthentication(responseBody, true, currentIdentity.Name);
}
}
[ConditionalTheory(nameof(DomainJoinedTestsEnabled))]
[InlineData(false)]
[InlineData(true)]
public async Task UseDefaultCredentials_SetTrueAndServerOffersMultipleSchemes_Ok(bool useProxy)
{
var handler = new HttpClientHandler { UseProxy = useProxy, UseDefaultCredentials = true };
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(MultipleSchemesAuthenticatedServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseBody = await response.Content.ReadAsStringAsync();
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
_output.WriteLine("currentIdentity={0}", currentIdentity.Name);
VerifyAuthentication(responseBody, true, currentIdentity.Name);
}
}
[ConditionalTheory(nameof(DomainJoinedTestsEnabled))]
[InlineData(false)]
[InlineData(true)]
public async Task Credentials_SetToSpecificCredential_ConnectAsSpecificIdentity(bool useProxy)
{
var handler = new HttpClientHandler {
UseProxy = useProxy,
UseDefaultCredentials = false,
Credentials = _specificCredential };
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(AuthenticatedServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseBody = await response.Content.ReadAsStringAsync();
VerifyAuthentication(responseBody, true, SpecificDomain + "\\" + SpecificUserName);
}
}
[ConditionalTheory(nameof(DomainJoinedTestsEnabled))]
[InlineData(false)]
[InlineData(true)]
public async Task Credentials_SetToWrappedDefaultCredential_ConnectAsCurrentIdentity(bool useProxy)
{
var handler = new HttpClientHandler();
handler.UseProxy = useProxy;
handler.Credentials = new CredentialWrapper
{
InnerCredentials = CredentialCache.DefaultCredentials
};
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(AuthenticatedServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
string responseBody = await response.Content.ReadAsStringAsync();
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
_output.WriteLine("currentIdentity={0}", currentIdentity.Name);
VerifyAuthentication(responseBody, true, currentIdentity.Name);
}
}
[ConditionalFact(nameof(DomainJoinedTestsEnabled))]
public async Task Proxy_UseAuthenticatedProxyWithNoCredentials_ProxyAuthenticationRequired()
{
var handler = new HttpClientHandler();
handler.Proxy = new AuthenticatedProxy(null);
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.RemoteEchoServer))
{
Assert.Equal(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode);
}
}
[ConditionalFact(nameof(DomainJoinedTestsEnabled))]
public async Task Proxy_UseAuthenticatedProxyWithDefaultCredentials_OK()
{
var handler = new HttpClientHandler();
handler.Proxy = new AuthenticatedProxy(CredentialCache.DefaultCredentials);
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.RemoteEchoServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
[ConditionalFact(nameof(DomainJoinedTestsEnabled))]
public async Task Proxy_UseAuthenticatedProxyWithWrappedDefaultCredentials_OK()
{
ICredentials wrappedCreds = new CredentialWrapper
{
InnerCredentials = CredentialCache.DefaultCredentials
};
var handler = new HttpClientHandler();
handler.Proxy = new AuthenticatedProxy(wrappedCreds);
using (var client = new HttpClient(handler))
using (HttpResponseMessage response = await client.GetAsync(HttpTestServers.RemoteEchoServer))
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
private void VerifyAuthentication(string response, bool authenticated, string user)
{
// Convert all strings to lowercase to compare. Windows treats domain and username as case-insensitive.
response = response.ToLower();
user = user.ToLower();
_output.WriteLine(response);
if (!authenticated)
{
Assert.True(
TestHelper.JsonMessageContainsKeyValue(response, "authenticated", "false"),
"authenticated == false");
}
else
{
Assert.True(
TestHelper.JsonMessageContainsKeyValue(response, "authenticated", "true"),
"authenticated == true");
Assert.True(
TestHelper.JsonMessageContainsKeyValue(response, "user", user),
$"user == {user}");
}
}
private class CredentialWrapper : ICredentials
{
public ICredentials InnerCredentials { get; set; }
public NetworkCredential GetCredential(Uri uri, String authType) =>
InnerCredentials?.GetCredential(uri, authType);
}
private class AuthenticatedProxy : IWebProxy
{
ICredentials _credentials;
Uri _proxyUri;
public AuthenticatedProxy(ICredentials credentials)
{
_credentials = credentials;
string host = TestSettings.Http.DomainJoinedProxyHost;
Assert.False(string.IsNullOrEmpty(host), "TestSettings.Http.DomainJoinedProxyHost must specify proxy hostname");
string portString = TestSettings.Http.DomainJoinedProxyPort;
Assert.False(string.IsNullOrEmpty(portString), "TestSettings.Http.DomainJoinedProxyPort must specify proxy port number");
int port;
Assert.True(int.TryParse(portString, out port), "TestSettings.Http.DomainJoinedProxyPort must be a valid port number");
_proxyUri = new Uri(string.Format("http://{0}:{1}", host, port));
}
public ICredentials Credentials
{
get
{
return _credentials;
}
set
{
throw new NotImplementedException();
}
}
public Uri GetProxy(Uri destination)
{
return _proxyUri;
}
public bool IsBypassed(Uri host)
{
return false;
}
}
}
}