diff options
-rw-r--r-- | src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs | 4 | ||||
-rw-r--r-- | src/DataProtection/DataProtection/test/HostingTests.cs | 23 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs b/src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs index 5e8ddbac3b..86c6f359c8 100644 --- a/src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs +++ b/src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.IO; using Microsoft.AspNetCore.DataProtection.Infrastructure; using Microsoft.Extensions.Hosting; @@ -20,5 +21,6 @@ internal class HostingApplicationDiscriminator : IApplicationDiscriminator _hosting = hosting; } - public string? Discriminator => _hosting?.ContentRootPath; + // Note: ContentRootPath behavior depends on the version, sometimes it has a trailing slash, we normalize by default by removing a trailing slash + public string? Discriminator => _hosting?.ContentRootPath?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); } diff --git a/src/DataProtection/DataProtection/test/HostingTests.cs b/src/DataProtection/DataProtection/test/HostingTests.cs index b3a59177c6..84c68bc0b7 100644 --- a/src/DataProtection/DataProtection/test/HostingTests.cs +++ b/src/DataProtection/DataProtection/test/HostingTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.DataProtection.Infrastructure; using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; @@ -17,6 +18,28 @@ namespace Microsoft.AspNetCore.DataProtection.Test; public class HostingTests { [Fact] + public void DefaultApplicationDiscriminatorTrimsTrailingSlash() + { + var builder = new WebHostBuilder() + .UseStartup<TestStartup>() + .ConfigureServices(s => s.AddDataProtection()); + + using (var host = builder.Build()) + { + var contentRootPath = host.Services.GetRequiredService<IWebHostEnvironment>().ContentRootPath; + Assert.True(contentRootPath.EndsWith(Path.DirectorySeparatorChar), "expected contentRootPath to end with a slash"); + + var appDisc = host.Services.GetRequiredService<IApplicationDiscriminator>().Discriminator; + Assert.False(appDisc.EndsWith(Path.DirectorySeparatorChar), "expected appDiscriminator to have slash trimmed"); + Assert.False(appDisc.EndsWith(Path.AltDirectorySeparatorChar), "expected appDiscriminator to have slash trimmed"); + + var appId = host.Services.GetApplicationUniqueIdentifier(); + Assert.False(appId.EndsWith(Path.DirectorySeparatorChar), "expected appId to have slash trimmed"); + Assert.False(appId.EndsWith(Path.AltDirectorySeparatorChar), "expected appId to have slash trimmed"); + } + } + + [Fact] public async Task WebhostLoadsKeyRingBeforeServerStarts() { var tcs = new TaskCompletionSource(); |