Repository: dotnet/runtime
File: src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs (C#)

Code before:
    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 => s_UsePortInSpn.Value;
        private static readonly Lazy<bool> s_UsePortInSpn = new Lazy<bool>(GetUsePortInSpn);

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

            // 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);
            }

            return false;
        }

        private static Task<HttpResponseMessage> InnerSendAsync(HttpRequestMessage request, bool async, bool isProxyAuth, HttpConnectionPool pool, HttpConnection connection, CancellationToken cancellationToken)
        {
            return isProxyAuth ?
                connection.SendAsyncCore(request, async, cancellationToken) :
                pool.SendWithNtProxyAuthAsync(connection, request, async, cancellationToken);
        }

        private static bool ProxySupportsConnectionAuth(HttpResponseMessage response)
        {
            if (!response.Headers.TryGetValues(KnownHeaders.ProxySupport.Descriptor, out IEnumerable<string>? values))
            {
                return false;
            }

            foreach (string v in values)
            {
                if (v == "Session-Based-Authentication")
                {
                    return true;
                }
            }

            return false;
        }

Review comment (suggestion):
```suggestion
        private static volatile int s_usePortInSpn = -1;

        private static bool UsePortInSpn
        {
            get
            {
                int usePortInSpn = s_usePortInSpn;
                if (usePortInSpn != -1)
                {
                    return usePortInSpn != 0;
                }
                
                // First check for the AppContext switch, giving it priority over the environment variable.
                if (AppContext.TryGetSwitch(UsePortInSpnCtxSwitch, out bool disabled))
                {
                    s_usePortInSpn = disabled;
                }
                else
                {
                    // AppContext switch wasn't used. Check the environment variable.
                   s_usePortInSpn =
                       Environment.GetEnvironmentVariable(UsePortInSpnEnvironmentVariable) is string envVar &&
                       (envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase);
                }
                
                return s_usePortInSpn != 0;
            }
        }
```

Code after:
    internal static partial class AuthenticationHelper
    {
        private const string UsePortInSpnCtxSwitch = "System.Net.Http.UsePortInSpn";
        private const string UsePortInSpnEnvironmentVariable = "DOTNET_SYSTEM_NET_HTTP_USEPORTINSPN";

        private static volatile int s_usePortInSpn = -1;

        private static bool UsePortInSpn
        {
            get
            {
                int usePortInSpn = s_usePortInSpn;
                if (usePortInSpn != -1)
                {
                    return usePortInSpn != 0;
                }

                // First check for the AppContext switch, giving it priority over the environment variable.
                if (AppContext.TryGetSwitch(UsePortInSpnCtxSwitch, out bool value))
                {
                    s_usePortInSpn = value ? 1 : 0;
                }
                else
                {
                    // AppContext switch wasn't used. Check the environment variable.
                   s_usePortInSpn =
                       Environment.GetEnvironmentVariable(UsePortInSpnEnvironmentVariable) is string envVar &&
                       (envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase)) ? 1 : 0;
                }

                return s_usePortInSpn != 0;
            }
        }

        private static Task<HttpResponseMessage> InnerSendAsync(HttpRequestMessage request, bool async, bool isProxyAuth, HttpConnectionPool pool, HttpConnection connection, CancellationToken cancellationToken)
        {
            return isProxyAuth ?
                connection.SendAsyncCore(request, async, cancellationToken) :
                pool.SendWithNtProxyAuthAsync(connection, request, async, cancellationToken);
        }

        private static bool ProxySupportsConnectionAuth(HttpResponseMessage response)
        {
            if (!response.Headers.TryGetValues(KnownHeaders.ProxySupport.Descriptor, out IEnumerable<string>? values))
            {
                return false;
            }

            foreach (string v in values)
            {
                if (v == "Session-Based-Authentication")