From e717a8443e552f02fc96bd2c6733da3b90e34d6a Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Mon, 17 Dec 2018 12:30:28 -0800 Subject: [PATCH] Update last few projects to target `netcoreapp3.0` (#5128) - #3754 - remove `#if`'s for multi-targeting where source is never multi-targeted - left `StreamPipeReader`, `StreamPipeWriter` and their test classes alone because they're moving to CoreFx --- .../src/PBKDF2/NetCorePbkdf2Provider.cs | 8 -- .../src/PBKDF2/Pbkdf2Util.cs | 10 -- .../test/Pbkdf2Tests.cs | 6 -- .../test/AnonymousImpersonation.cs | 92 ------------------- .../test/TypeForwardingActivatorTests.cs | 14 --- .../XmlEncryption/DpapiXmlEncryptionTests.cs | 24 ----- .../src/Microsoft.AspNetCore.JsonPatch.csproj | 2 +- ...icrosoft.AspNetCore.JsonPatch.Tests.csproj | 2 +- .../ZipManifestGenerator.csproj | 2 +- .../CertificateManager.cs | 9 +- .../EnsureCertificateResult.cs | 4 - .../test/CertificateManagerTests.cs | 4 - 12 files changed, 4 insertions(+), 173 deletions(-) delete mode 100644 src/DataProtection/DataProtection/test/AnonymousImpersonation.cs diff --git a/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/NetCorePbkdf2Provider.cs b/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/NetCorePbkdf2Provider.cs index a8ce1772eb..ace4fd2be1 100644 --- a/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/NetCorePbkdf2Provider.cs +++ b/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/NetCorePbkdf2Provider.cs @@ -1,9 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#if NETCOREAPP3_0 -// Rfc2898DeriveBytes in .NET Standard 2.0 only supports SHA1 - using System; using System.Diagnostics; using System.Security.Cryptography; @@ -64,8 +61,3 @@ namespace Microsoft.AspNetCore.Cryptography.KeyDerivation.PBKDF2 } } } - -#elif NETSTANDARD2_0 -#else -#error Update target frameworks -#endif diff --git a/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/Pbkdf2Util.cs b/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/Pbkdf2Util.cs index d8139c92f7..7cbda18bdd 100644 --- a/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/Pbkdf2Util.cs +++ b/src/DataProtection/Cryptography.KeyDerivation/src/PBKDF2/Pbkdf2Util.cs @@ -25,22 +25,12 @@ namespace Microsoft.AspNetCore.Cryptography.KeyDerivation.PBKDF2 // acceptable implementation return new Win7Pbkdf2Provider(); } -#if NETCOREAPP3_0 else { // fastest implementation on .NET Core for Linux/macOS. // Not supported on .NET Framework return new NetCorePbkdf2Provider(); } -#elif NETSTANDARD2_0 - else - { - // slowest implementation - return new ManagedPbkdf2Provider(); - } -#else -#error Update target frameworks -#endif } } } diff --git a/src/DataProtection/Cryptography.KeyDerivation/test/Pbkdf2Tests.cs b/src/DataProtection/Cryptography.KeyDerivation/test/Pbkdf2Tests.cs index a45f5e24ce..295dc819c4 100644 --- a/src/DataProtection/Cryptography.KeyDerivation/test/Pbkdf2Tests.cs +++ b/src/DataProtection/Cryptography.KeyDerivation/test/Pbkdf2Tests.cs @@ -12,9 +12,6 @@ namespace Microsoft.AspNetCore.Cryptography.KeyDerivation { public class Pbkdf2Tests { - -#if NET461 -#elif NETCOREAPP3_0 // The 'numBytesRequested' parameters below are chosen to exercise code paths where // this value straddles the digest length of the PRF. We only use 5 iterations so // that our unit tests are fast. @@ -60,9 +57,6 @@ namespace Microsoft.AspNetCore.Cryptography.KeyDerivation var salt = Encoding.UTF8.GetBytes("abcdefghijkl"); RunTest_WithLongPassword_Impl(salt, "NGJtFzYUaaSxu+3ZsMeZO5d/qPJDUYW4caLkFlaY0cLSYdh1PN4+nHUVp4pUUubJWu3UeXNMnHKNDfnn8GMfnDVrAGTv1lldszsvUJ0JQ6p4+daQEYBc//Tj/ejuB3luwW0IinyE7U/ViOQKbfi5pCZFMQ0FFx9I+eXRlyT+I74="); } -#else -#error Update target framework -#endif // The 'numBytesRequested' parameters below are chosen to exercise code paths where // this value straddles the digest length of the PRF. We only use 5 iterations so diff --git a/src/DataProtection/DataProtection/test/AnonymousImpersonation.cs b/src/DataProtection/DataProtection/test/AnonymousImpersonation.cs deleted file mode 100644 index e142b698e7..0000000000 --- a/src/DataProtection/DataProtection/test/AnonymousImpersonation.cs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -#if NET461 -using System; -using System.Runtime.ConstrainedExecution; -using System.Runtime.InteropServices; -using Microsoft.AspNetCore.Cryptography; -using Microsoft.Win32.SafeHandles; - -namespace Microsoft.AspNetCore.DataProtection -{ - /// - /// Helpers for working with the anonymous Windows identity. - /// - internal static class AnonymousImpersonation - { - /// - /// Performs an action while impersonated under the anonymous user (NT AUTHORITY\ANONYMOUS LOGIN). - /// - public static void Run(Action callback) - { - using (var threadHandle = ThreadHandle.OpenCurrentThreadHandle()) - { - bool impersonated = false; - try - { - impersonated = ImpersonateAnonymousToken(threadHandle); - if (!impersonated) - { - Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); - } - callback(); - } - finally - { - if (impersonated && !RevertToSelf()) - { - Environment.FailFast("RevertToSelf() returned false!"); - } - } - } - } - - [DllImport("advapi32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)] - private static extern bool ImpersonateAnonymousToken([In] ThreadHandle ThreadHandle); - - [DllImport("advapi32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)] - private static extern bool RevertToSelf(); - - private sealed class ThreadHandle : SafeHandleZeroOrMinusOneIsInvalid - { - private ThreadHandle() - : base(ownsHandle: true) - { - } - - public static ThreadHandle OpenCurrentThreadHandle() - { - const int THREAD_ALL_ACCESS = 0x1FFFFF; - var handle = OpenThread( - dwDesiredAccess: THREAD_ALL_ACCESS, - bInheritHandle: false, -#pragma warning disable CS0618 // Type or member is obsolete - dwThreadId: (uint)AppDomain.GetCurrentThreadId()); -#pragma warning restore CS0618 // Type or member is obsolete - CryptoUtil.AssertSafeHandleIsValid(handle); - return handle; - } - - protected override bool ReleaseHandle() - { - return CloseHandle(handle); - } - - [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] - [DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)] - private static extern bool CloseHandle( - [In] IntPtr hObject); - - [DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)] - private static extern ThreadHandle OpenThread( - [In] uint dwDesiredAccess, - [In] bool bInheritHandle, - [In] uint dwThreadId); - } - } -} -#elif NETCOREAPP3_0 -#else -#error Target framework needs to be updated -#endif diff --git a/src/DataProtection/DataProtection/test/TypeForwardingActivatorTests.cs b/src/DataProtection/DataProtection/test/TypeForwardingActivatorTests.cs index e7df5cfeae..d135935681 100644 --- a/src/DataProtection/DataProtection/test/TypeForwardingActivatorTests.cs +++ b/src/DataProtection/DataProtection/test/TypeForwardingActivatorTests.cs @@ -103,21 +103,7 @@ namespace Microsoft.AspNetCore.DataProtection [MemberData(nameof(AssemblyVersions))] public void CreateInstance_ForwardsAcrossVersionChanges(Version version) { -#if NET461 - // run this test in an appdomain without testhost's custom assembly resolution hooks - var setupInfo = new AppDomainSetup - { - ApplicationBase = AppDomain.CurrentDomain.BaseDirectory, - ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, - }; - var domain = AppDomain.CreateDomain("TestDomain", null, setupInfo); - var wrappedTestClass = (TypeForwardingActivatorTests)domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName, typeof(TypeForwardingActivatorTests).FullName); - wrappedTestClass.CreateInstance_ForwardsAcrossVersionChangesImpl(version); -#elif NETCOREAPP3_0 CreateInstance_ForwardsAcrossVersionChangesImpl(version); -#else -#error Target framework should be updated -#endif } private void CreateInstance_ForwardsAcrossVersionChangesImpl(Version newVersion) diff --git a/src/DataProtection/DataProtection/test/XmlEncryption/DpapiXmlEncryptionTests.cs b/src/DataProtection/DataProtection/test/XmlEncryption/DpapiXmlEncryptionTests.cs index 7274d846ad..c6b4aa208a 100644 --- a/src/DataProtection/DataProtection/test/XmlEncryption/DpapiXmlEncryptionTests.cs +++ b/src/DataProtection/DataProtection/test/XmlEncryption/DpapiXmlEncryptionTests.cs @@ -33,29 +33,5 @@ namespace Microsoft.AspNetCore.DataProtection.XmlEncryption var roundTrippedElement = decryptor.Decrypt(encryptedXmlInfo.EncryptedElement); XmlAssert.Equal(originalXml, roundTrippedElement); } - -#if NET461 - [ConditionalFact] - [ConditionalRunTestOnlyOnWindows] - public void Encrypt_CurrentUser_Decrypt_ImpersonatedAsAnonymous_Fails() - { - // Arrange - var originalXml = XElement.Parse(@""); - var encryptor = new DpapiXmlEncryptor(protectToLocalMachine: false, loggerFactory: NullLoggerFactory.Instance); - var decryptor = new DpapiXmlDecryptor(); - - // Act & assert - run through encryptor and make sure we get back an obfuscated element - var encryptedXmlInfo = encryptor.Encrypt(originalXml); - Assert.Equal(typeof(DpapiXmlDecryptor), encryptedXmlInfo.DecryptorType); - Assert.DoesNotContain("265ee4ea-ade2-43b1-b706-09b259e58b6b", encryptedXmlInfo.EncryptedElement.ToString(), StringComparison.OrdinalIgnoreCase); - - // Act & assert - run through decryptor (while impersonated as anonymous) and verify failure - ExceptionAssert2.ThrowsCryptographicException(() => - AnonymousImpersonation.Run(() => decryptor.Decrypt(encryptedXmlInfo.EncryptedElement))); - } -#elif NETCOREAPP3_0 -#else -#error Target framework needs to be updated -#endif } } diff --git a/src/Features/JsonPatch/src/Microsoft.AspNetCore.JsonPatch.csproj b/src/Features/JsonPatch/src/Microsoft.AspNetCore.JsonPatch.csproj index f6f11fd3b3..b543615a88 100644 --- a/src/Features/JsonPatch/src/Microsoft.AspNetCore.JsonPatch.csproj +++ b/src/Features/JsonPatch/src/Microsoft.AspNetCore.JsonPatch.csproj @@ -2,7 +2,7 @@ ASP.NET Core support for JSON PATCH. - netstandard2.0 + netcoreapp3.0 $(NoWarn);CS1591 true aspnetcore;json;jsonpatch diff --git a/src/Features/JsonPatch/test/Microsoft.AspNetCore.JsonPatch.Tests.csproj b/src/Features/JsonPatch/test/Microsoft.AspNetCore.JsonPatch.Tests.csproj index 28d501111a..1a3c83556c 100644 --- a/src/Features/JsonPatch/test/Microsoft.AspNetCore.JsonPatch.Tests.csproj +++ b/src/Features/JsonPatch/test/Microsoft.AspNetCore.JsonPatch.Tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.0;net461 + netcoreapp3.0 diff --git a/src/PackageArchive/ZipManifestGenerator/ZipManifestGenerator.csproj b/src/PackageArchive/ZipManifestGenerator/ZipManifestGenerator.csproj index b7d3a43622..403e8cb58e 100644 --- a/src/PackageArchive/ZipManifestGenerator/ZipManifestGenerator.csproj +++ b/src/PackageArchive/ZipManifestGenerator/ZipManifestGenerator.csproj @@ -2,7 +2,7 @@ exe - netcoreapp2.1 + netcoreapp3.0 false 7.1 false diff --git a/src/Shared/CertificateGeneration/CertificateManager.cs b/src/Shared/CertificateGeneration/CertificateManager.cs index 26639572e4..17e0d52e4d 100644 --- a/src/Shared/CertificateGeneration/CertificateManager.cs +++ b/src/Shared/CertificateGeneration/CertificateManager.cs @@ -32,18 +32,14 @@ namespace Microsoft.AspNetCore.Certificates.Generation private const string MacOSSystemKeyChain = "/Library/Keychains/System.keychain"; private static readonly string MacOSUserKeyChain = Environment.GetEnvironmentVariable("HOME") + "/Library/Keychains/login.keychain-db"; private const string MacOSFindCertificateCommandLine = "security"; -#if NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 private static readonly string MacOSFindCertificateCommandLineArgumentsFormat = "find-certificate -c {0} -a -Z -p " + MacOSSystemKeyChain; -#endif private const string MacOSFindCertificateOutputRegex = "SHA-1 hash: ([0-9A-Z]+)"; private const string MacOSRemoveCertificateTrustCommandLine = "sudo"; private const string MacOSRemoveCertificateTrustCommandLineArgumentsFormat = "security remove-trusted-cert -d {0}"; private const string MacOSDeleteCertificateCommandLine = "sudo"; private const string MacOSDeleteCertificateCommandLineArgumentsFormat = "security delete-certificate -Z {0} {1}"; private const string MacOSTrustCertificateCommandLine = "sudo"; -#if NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 private static readonly string MacOSTrustCertificateCommandLineArguments = "security add-trusted-cert -d -r trustRoot -k " + MacOSSystemKeyChain + " "; -#endif private const int UserCancelledErrorCode = 1223; public IList ListCertificates( @@ -153,8 +149,6 @@ namespace Microsoft.AspNetCore.Certificates.Generation } } -#if NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 - public X509Certificate2 CreateAspNetCoreHttpsDevelopmentCertificate(DateTimeOffset notBefore, DateTimeOffset notAfter, string subjectOverride, DiagnosticInformation diagnostics = null) { var subject = new X500DistinguishedName(subjectOverride ?? LocalhostHttpsDistinguishedName); @@ -739,7 +733,7 @@ namespace Microsoft.AspNetCore.Certificates.Generation return result; } - // This is just to avoid breaking changes across repos. + // This is just to avoid breaking changes across repos. // Will be renamed back to EnsureAspNetCoreHttpsDevelopmentCertificate once updates are made elsewhere. public DetailedEnsureCertificateResult EnsureAspNetCoreHttpsDevelopmentCertificate2( DateTimeOffset notBefore, @@ -893,7 +887,6 @@ namespace Microsoft.AspNetCore.Certificates.Generation public EnsureCertificateResult ResultCode { get; set; } public DiagnosticInformation Diagnostics { get; set; } = new DiagnosticInformation(); } -#endif internal class DiagnosticInformation { diff --git a/src/Shared/CertificateGeneration/EnsureCertificateResult.cs b/src/Shared/CertificateGeneration/EnsureCertificateResult.cs index 2106297fae..4676d7b3aa 100644 --- a/src/Shared/CertificateGeneration/EnsureCertificateResult.cs +++ b/src/Shared/CertificateGeneration/EnsureCertificateResult.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#if NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 - namespace Microsoft.AspNetCore.Certificates.Generation { internal enum EnsureCertificateResult @@ -16,5 +14,3 @@ namespace Microsoft.AspNetCore.Certificates.Generation UserCancelledTrustStep } } - -#endif diff --git a/src/Tools/FirstRunCertGenerator/test/CertificateManagerTests.cs b/src/Tools/FirstRunCertGenerator/test/CertificateManagerTests.cs index 0979eba21b..52caae046d 100644 --- a/src/Tools/FirstRunCertGenerator/test/CertificateManagerTests.cs +++ b/src/Tools/FirstRunCertGenerator/test/CertificateManagerTests.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#if NETCOREAPP3_0 - using System; using System.IO; using System.Linq; @@ -284,5 +282,3 @@ namespace Microsoft.AspNetCore.Certificates.Generation.Tests } } } - -#endif