From 5e81b893785fde37b5c91f0228288f12b3bdfd3b Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 25 Apr 2016 14:07:53 -0700 Subject: [PATCH] Replace IApplicationEnvironment with IHostingEnvironment --- .../DataProtectionExtensions.cs | 36 ------------- .../project.json | 7 +-- .../DataProtectionExtensions.cs | 51 +++++++++++++++++++ .../project.json | 3 ++ .../DataProtectionExtensionsTests.cs | 32 ------------ .../DataProtectionExtensionsTests.cs | 44 ++++++++++++++++ 6 files changed, 102 insertions(+), 71 deletions(-) create mode 100644 src/Microsoft.AspNetCore.DataProtection/DataProtectionExtensions.cs create mode 100644 test/Microsoft.AspNetCore.DataProtection.Test/DataProtectionExtensionsTests.cs diff --git a/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionExtensions.cs b/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionExtensions.cs index 9096a871d7..78c72193e2 100644 --- a/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionExtensions.cs +++ b/src/Microsoft.AspNetCore.DataProtection.Abstractions/DataProtectionExtensions.cs @@ -3,11 +3,8 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Diagnostics; -using Microsoft.AspNetCore.DataProtection.Infrastructure; using Microsoft.AspNetCore.DataProtection.Abstractions; -using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNetCore.DataProtection { @@ -98,39 +95,6 @@ namespace Microsoft.AspNetCore.DataProtection return protector ?? CryptoUtil.Fail("CreateProtector returned null."); } - /// - /// Returns a unique identifier for this application. - /// - /// The application-level . - /// A unique application identifier, or null if is null - /// or cannot provide a unique application identifier. - /// - /// - /// The returned identifier should be stable for repeated runs of this same application on - /// this machine. Additionally, the identifier is only unique within the scope of a single - /// machine, e.g., two different applications on two different machines may return the same - /// value. - /// - /// - /// This identifier may contain security-sensitive information such as physical file paths, - /// configuration settings, or other machine-specific information. Callers should take - /// special care not to disclose this information to untrusted entities. - /// - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public static string GetApplicationUniqueIdentifier(this IServiceProvider services) - { - string discriminator = (services?.GetService(typeof(IApplicationDiscriminator)) as IApplicationDiscriminator)?.Discriminator; - if (discriminator == null) - { - discriminator = (services?.GetService(typeof(IApplicationEnvironment)) as IApplicationEnvironment)?.ApplicationBasePath; - } - - // Remove whitespace and homogenize empty -> null - discriminator = discriminator?.Trim(); - return (String.IsNullOrEmpty(discriminator)) ? null : discriminator; - } - /// /// Retrieves an from an . /// diff --git a/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json b/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json index f043edf9ae..34fe5048c7 100644 --- a/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json +++ b/src/Microsoft.AspNetCore.DataProtection.Abstractions/project.json @@ -13,15 +13,16 @@ "Microsoft.AspNetCore.DataProtection.Sources": { "type": "build", "version": "1.0.0-*" - }, - "Microsoft.Extensions.PlatformAbstractions": "1.0.0-*" + } }, "frameworks": { "net451": {}, "netstandard1.3": { "dependencies": { "System.ComponentModel": "4.0.1-*", - "System.Diagnostics.Debug": "4.0.11-*" + "System.Diagnostics.Debug": "4.0.11-*", + "System.Resources.ResourceManager": "4.0.1-*", + "System.Runtime.Extensions": "4.1.0-*" } } }, diff --git a/src/Microsoft.AspNetCore.DataProtection/DataProtectionExtensions.cs b/src/Microsoft.AspNetCore.DataProtection/DataProtectionExtensions.cs new file mode 100644 index 0000000000..2624a5220e --- /dev/null +++ b/src/Microsoft.AspNetCore.DataProtection/DataProtectionExtensions.cs @@ -0,0 +1,51 @@ +// 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. + +using System; +using System.ComponentModel; +using Microsoft.AspNetCore.DataProtection.Infrastructure; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNetCore.DataProtection +{ + public static class DataProtectionExtensions + { + /// + /// Returns a unique identifier for this application. + /// + /// The application-level . + /// A unique application identifier, or null if is null + /// or cannot provide a unique application identifier. + /// + /// + /// The returned identifier should be stable for repeated runs of this same application on + /// this machine. Additionally, the identifier is only unique within the scope of a single + /// machine, e.g., two different applications on two different machines may return the same + /// value. + /// + /// + /// This identifier may contain security-sensitive information such as physical file paths, + /// configuration settings, or other machine-specific information. Callers should take + /// special care not to disclose this information to untrusted entities. + /// + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public static string GetApplicationUniqueIdentifier(this IServiceProvider services) + { + string discriminator = null; + if (services != null) + { + discriminator = services.GetService()?.Discriminator; + if (discriminator == null) + { + discriminator = services.GetService()?.ContentRootPath; + } + } + + // Remove whitespace and homogenize empty -> null + discriminator = discriminator?.Trim(); + return (string.IsNullOrEmpty(discriminator)) ? null : discriminator; + } + } +} diff --git a/src/Microsoft.AspNetCore.DataProtection/project.json b/src/Microsoft.AspNetCore.DataProtection/project.json index de143d2eb5..72904cc78f 100644 --- a/src/Microsoft.AspNetCore.DataProtection/project.json +++ b/src/Microsoft.AspNetCore.DataProtection/project.json @@ -16,6 +16,7 @@ "type": "build", "version": "1.0.0-*" }, + "Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0-*", "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-*", "Microsoft.Extensions.Logging.Abstractions": "1.0.0-*", "Microsoft.Extensions.Options": "1.0.0-*" @@ -34,6 +35,8 @@ "netstandard1.3": { "dependencies": { "Microsoft.Win32.Registry": "4.0.0-*", + "System.IO.FileSystem": "4.0.1-*", + "System.Reflection.Extensions": "4.0.1-*", "System.Security.Cryptography.X509Certificates": "4.1.0-*", "System.Security.Claims": "4.0.1-*", "System.Security.Principal.Windows": "4.0.0-*", diff --git a/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/DataProtectionExtensionsTests.cs b/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/DataProtectionExtensionsTests.cs index ab4294c607..dba42bc903 100644 --- a/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/DataProtectionExtensionsTests.cs +++ b/test/Microsoft.AspNetCore.DataProtection.Abstractions.Test/DataProtectionExtensionsTests.cs @@ -5,10 +5,8 @@ using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; -using Microsoft.AspNetCore.DataProtection.Infrastructure; using Microsoft.AspNetCore.DataProtection.Abstractions; using Microsoft.AspNetCore.Testing; -using Microsoft.Extensions.PlatformAbstractions; using Moq; using Xunit; @@ -108,36 +106,6 @@ namespace Microsoft.AspNetCore.DataProtection Assert.Same(finalExpectedProtector, retVal); } - [Theory] - [InlineData(" discriminator", "app-path ", "discriminator")] // normalized trim - [InlineData("", "app-path", null)] // app discriminator not null -> overrides app base path - [InlineData(null, "app-path ", "app-path")] // normalized trim - [InlineData(null, " ", null)] // normalized whitespace -> null - [InlineData(null, null, null)] // nothing provided at all - public void GetApplicationUniqueIdentifier(string appDiscriminator, string appBasePath, string expected) - { - // Arrange - var mockAppDiscriminator = new Mock(); - mockAppDiscriminator.Setup(o => o.Discriminator).Returns(appDiscriminator); - var mockAppEnvironment = new Mock(); - mockAppEnvironment.Setup(o => o.ApplicationBasePath).Returns(appBasePath); - var mockServiceProvider = new Mock(); - mockServiceProvider.Setup(o => o.GetService(typeof(IApplicationDiscriminator))).Returns(mockAppDiscriminator.Object); - mockServiceProvider.Setup(o => o.GetService(typeof(IApplicationEnvironment))).Returns(mockAppEnvironment.Object); - - // Act - string actual = mockServiceProvider.Object.GetApplicationUniqueIdentifier(); - - // Assert - Assert.Equal(expected, actual); - } - - [Fact] - public void GetApplicationUniqueIdentifier_NoServiceProvider_ReturnsNull() - { - Assert.Null(((IServiceProvider)null).GetApplicationUniqueIdentifier()); - } - [Fact] public void GetDataProtectionProvider_NoServiceFound_Throws() { diff --git a/test/Microsoft.AspNetCore.DataProtection.Test/DataProtectionExtensionsTests.cs b/test/Microsoft.AspNetCore.DataProtection.Test/DataProtectionExtensionsTests.cs new file mode 100644 index 0000000000..364d37ccac --- /dev/null +++ b/test/Microsoft.AspNetCore.DataProtection.Test/DataProtectionExtensionsTests.cs @@ -0,0 +1,44 @@ +// 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. + +using System; +using Microsoft.AspNetCore.DataProtection.Infrastructure; +using Microsoft.AspNetCore.Hosting; +using Moq; +using Xunit; + +namespace Microsoft.AspNetCore.DataProtection +{ + public class DataProtectionExtensionsTests + { + [Theory] + [InlineData(" discriminator", "app-path ", "discriminator")] // normalized trim + [InlineData("", "app-path", null)] // app discriminator not null -> overrides app base path + [InlineData(null, "app-path ", "app-path")] // normalized trim + [InlineData(null, " ", null)] // normalized whitespace -> null + [InlineData(null, null, null)] // nothing provided at all + public void GetApplicationUniqueIdentifier(string appDiscriminator, string appBasePath, string expected) + { + // Arrange + var mockAppDiscriminator = new Mock(); + mockAppDiscriminator.Setup(o => o.Discriminator).Returns(appDiscriminator); + var mockEnvironment = new Mock(); + mockEnvironment.Setup(o => o.ContentRootPath).Returns(appBasePath); + var mockServiceProvider = new Mock(); + mockServiceProvider.Setup(o => o.GetService(typeof(IApplicationDiscriminator))).Returns(mockAppDiscriminator.Object); + mockServiceProvider.Setup(o => o.GetService(typeof(IHostingEnvironment))).Returns(mockEnvironment.Object); + + // Act + string actual = mockServiceProvider.Object.GetApplicationUniqueIdentifier(); + + // Assert + Assert.Equal(expected, actual); + } + + [Fact] + public void GetApplicationUniqueIdentifier_NoServiceProvider_ReturnsNull() + { + Assert.Null(((IServiceProvider)null).GetApplicationUniqueIdentifier()); + } + } +}