Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,5 +21,6 @@ public HostingApplicationDiscriminator(IHostEnvironment hosting)
_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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be stored in a field at construction instead of computed of every invocation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know how IHostEnvironment implements ContentRootPath right? Seems like the caching would belong there

}
23 changes: 23 additions & 0 deletions src/DataProtection/DataProtection/test/HostingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,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()
{
Expand Down