Skip to content

Commit cf513d3

Browse files
authored
Normalize data protection app discriminator (#41095)
1 parent 98bc70b commit cf513d3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/DataProtection/DataProtection/src/Internal/HostingApplicationDiscriminator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.IO;
45
using Microsoft.AspNetCore.DataProtection.Infrastructure;
56
using Microsoft.Extensions.Hosting;
67

@@ -20,5 +21,6 @@ public HostingApplicationDiscriminator(IHostEnvironment hosting)
2021
_hosting = hosting;
2122
}
2223

23-
public string? Discriminator => _hosting?.ContentRootPath;
24+
// Note: ContentRootPath behavior depends on the version, sometimes it has a trailing slash, we normalize by default by removing a trailing slash
25+
public string? Discriminator => _hosting?.ContentRootPath?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
2426
}

src/DataProtection/DataProtection/test/HostingTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.DataProtection.Infrastructure;
56
using Microsoft.AspNetCore.DataProtection.KeyManagement.Internal;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.Hosting.Server;
@@ -16,6 +17,28 @@ namespace Microsoft.AspNetCore.DataProtection.Test;
1617

1718
public class HostingTests
1819
{
20+
[Fact]
21+
public void DefaultApplicationDiscriminatorTrimsTrailingSlash()
22+
{
23+
var builder = new WebHostBuilder()
24+
.UseStartup<TestStartup>()
25+
.ConfigureServices(s => s.AddDataProtection());
26+
27+
using (var host = builder.Build())
28+
{
29+
var contentRootPath = host.Services.GetRequiredService<IWebHostEnvironment>().ContentRootPath;
30+
Assert.True(contentRootPath.EndsWith(Path.DirectorySeparatorChar), "expected contentRootPath to end with a slash");
31+
32+
var appDisc = host.Services.GetRequiredService<IApplicationDiscriminator>().Discriminator;
33+
Assert.False(appDisc.EndsWith(Path.DirectorySeparatorChar), "expected appDiscriminator to have slash trimmed");
34+
Assert.False(appDisc.EndsWith(Path.AltDirectorySeparatorChar), "expected appDiscriminator to have slash trimmed");
35+
36+
var appId = host.Services.GetApplicationUniqueIdentifier();
37+
Assert.False(appId.EndsWith(Path.DirectorySeparatorChar), "expected appId to have slash trimmed");
38+
Assert.False(appId.EndsWith(Path.AltDirectorySeparatorChar), "expected appId to have slash trimmed");
39+
}
40+
}
41+
1942
[Fact]
2043
public async Task WebhostLoadsKeyRingBeforeServerStarts()
2144
{

0 commit comments

Comments
 (0)