Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHao Kung <HaoK@users.noreply.github.com>2022-04-21 23:58:48 +0300
committerGitHub <noreply@github.com>2022-04-21 23:58:48 +0300
commitcf513d3fed2c5918fa7059e0d0840d584db775fb (patch)
tree43acf3e800e5c45f0692372ccc1bbc0ed79a94b5
parent98bc70b04c5221d5758d0aefe4defa6721c49457 (diff)
Normalize data protection app discriminator (#41095)
-rw-r--r--src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs4
-rw-r--r--src/DataProtection/DataProtection/test/HostingTests.cs23
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();