Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make use of ports in SPN optional #57159

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feedback from review
  • Loading branch information
wfurt committed Aug 10, 2021
commit 919cb9094fc603c30d42c165425fbbc29819a36b
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ internal static partial class AuthenticationHelper
private const string UsePortInSpnCtxSwitch = "System.Net.Http.UsePortInSpn";
private const string UsePortInSpnEnvironmentVariable = "DOTNET_SYSTEM_NET_HTTP_USEPORTINSPN";

private static bool UsePortInSpn
private static bool UsePortInSpn => s_UsePortInSpn.Value;
private static readonly Lazy<bool> s_UsePortInSpn = new Lazy<bool>(GetUsePortInSpn);

private static bool GetUsePortInSpn()
{
get
// First check for the AppContext switch, giving it priority over the environment variable.
if (AppContext.TryGetSwitch(UsePortInSpnCtxSwitch, out bool disabled))
{
// First check for the AppContext switch, giving it priority over the environment variable.
if (AppContext.TryGetSwitch(UsePortInSpnCtxSwitch, out bool disabled))
{
return disabled;
}
return disabled;
}

// AppContext switch wasn't used. Check the environment variable.
string? envVar = Environment.GetEnvironmentVariable(UsePortInSpnEnvironmentVariable);
// AppContext switch wasn't used. Check the environment variable.
string? envVar = Environment.GetEnvironmentVariable(UsePortInSpnEnvironmentVariable);

if (envVar is not null)
{
return envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase);
}
if (envVar is not null)
{
return envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase);
}

return false;
}
return false;
}

private static Task<HttpResponseMessage> InnerSendAsync(HttpRequestMessage request, bool async, bool isProxyAuth, HttpConnectionPool pool, HttpConnection connection, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Net.Test.Common;
using System.Threading.Tasks;

using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Net.Http.Enterprise.Tests
Expand All @@ -20,16 +20,22 @@ public class HttpClientAuthenticationTest
[InlineData(EnterpriseTestConfiguration.DigestAuthWebServer, true)]
[InlineData(EnterpriseTestConfiguration.DigestAuthWebServer, false)]
[InlineData(EnterpriseTestConfiguration.NtlmAuthWebServer, true)]
public async Task HttpClient_ValidAuthentication_Success(string url, bool useDomain, bool useAltPort = false)
public void HttpClient_ValidAuthentication_Success(string url, bool useDomain, bool useAltPort = false)
{
// This is safe as we have no parallel tests
AppContext.SetSwitch(AppContextSettingName, useAltPort);
using var handler = new HttpClientHandler();
handler.Credentials = useDomain ? EnterpriseTestConfiguration.ValidDomainNetworkCredentials : EnterpriseTestConfiguration.ValidNetworkCredentials;
using var client = new HttpClient(handler);
RemoteExecutor.Invoke((url, useAltPort, useDomain) =>
{
// This is safe as we have no parallel tests
if (!string.IsNullOrEmpty(useAltPort))
{
AppContext.SetSwitch(AppContextSettingName, true);
}
using var handler = new HttpClientHandler();
handler.Credentials = string.IsNullOrEmpty(useDomain) ? EnterpriseTestConfiguration.ValidNetworkCredentials : EnterpriseTestConfiguration.ValidDomainNetworkCredentials;
using var client = new HttpClient(handler);

using HttpResponseMessage response = await client.GetAsync(url);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
using HttpResponseMessage response = client.GetAsync(url).GetAwaiter().GetResult();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}, url, useAltPort ? "true" : "" , useDomain ? "true" : "").Dispose();
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/416")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<ItemGroup>
<Compile Include="HttpClientAuthenticationTest.cs" />

<Compile Include="$(CommonTestPath)System\Net\EnterpriseTests\EnterpriseTestConfiguration.cs"
Link="Common\System\Net\EnterpriseTests\EnterpriseTestConfiguration.cs" />
</ItemGroup>
</Project>
</Project>