Replace IApplicationEnvironment with IHostingEnvironment
This commit is contained in:
parent
36d84468fa
commit
5e81b89378
|
|
@ -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<IDataProtector>("CreateProtector returned null.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a unique identifier for this application.
|
||||
/// </summary>
|
||||
/// <param name="services">The application-level <see cref="IServiceProvider"/>.</param>
|
||||
/// <returns>A unique application identifier, or null if <paramref name="services"/> is null
|
||||
/// or cannot provide a unique application identifier.</returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
[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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves an <see cref="IDataProtectionProvider"/> from an <see cref="IServiceProvider"/>.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -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-*"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a unique identifier for this application.
|
||||
/// </summary>
|
||||
/// <param name="services">The application-level <see cref="IServiceProvider"/>.</param>
|
||||
/// <returns>A unique application identifier, or null if <paramref name="services"/> is null
|
||||
/// or cannot provide a unique application identifier.</returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public static string GetApplicationUniqueIdentifier(this IServiceProvider services)
|
||||
{
|
||||
string discriminator = null;
|
||||
if (services != null)
|
||||
{
|
||||
discriminator = services.GetService<IApplicationDiscriminator>()?.Discriminator;
|
||||
if (discriminator == null)
|
||||
{
|
||||
discriminator = services.GetService<IHostingEnvironment>()?.ContentRootPath;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove whitespace and homogenize empty -> null
|
||||
discriminator = discriminator?.Trim();
|
||||
return (string.IsNullOrEmpty(discriminator)) ? null : discriminator;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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-*",
|
||||
|
|
|
|||
|
|
@ -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<IApplicationDiscriminator>();
|
||||
mockAppDiscriminator.Setup(o => o.Discriminator).Returns(appDiscriminator);
|
||||
var mockAppEnvironment = new Mock<IApplicationEnvironment>();
|
||||
mockAppEnvironment.Setup(o => o.ApplicationBasePath).Returns(appBasePath);
|
||||
var mockServiceProvider = new Mock<IServiceProvider>();
|
||||
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()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<IApplicationDiscriminator>();
|
||||
mockAppDiscriminator.Setup(o => o.Discriminator).Returns(appDiscriminator);
|
||||
var mockEnvironment = new Mock<IHostingEnvironment>();
|
||||
mockEnvironment.Setup(o => o.ContentRootPath).Returns(appBasePath);
|
||||
var mockServiceProvider = new Mock<IServiceProvider>();
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue