Skip to content

Fix endpoints in StaticFileAuth sample #43352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2022
Merged
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
23 changes: 7 additions & 16 deletions src/Security/samples/StaticFilesAuth/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;

namespace StaticFilesAuth;

Expand Down Expand Up @@ -53,7 +42,7 @@ public void ConfigureServices(IServiceCollection services)
{
return false;
}
if (context.Resource is Endpoint endpoint)
if (context.Resource is HttpContext httpContext && httpContext.GetEndpoint() is Endpoint endpoint)
{
var userPath = Path.Combine(usersPath, userName);

Expand Down Expand Up @@ -134,12 +123,14 @@ private static void SetFileEndpoint(HttpContext context, PhysicalFileProvider fi
var fileSystemPath = GetFileSystemPath(files, context.Request.Path);
if (fileSystemPath != null)
{
var metadata = new List<object>();
metadata.Add(new DirectoryInfo(Path.GetDirectoryName(fileSystemPath)));
metadata.Add(new AuthorizeAttribute(policy));
var metadata = new List<object>
{
new DirectoryInfo(Path.GetDirectoryName(fileSystemPath)),
new AuthorizeAttribute(policy)
};

var endpoint = new Endpoint(
c => throw new InvalidOperationException("Static file middleware should return file request."),
requestDelegate: null,
Copy link
Member

Choose a reason for hiding this comment

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

This led me to this:

// Return true because we only want to run if there is no endpoint delegate.
private static bool ValidateNoEndpointDelegate(HttpContext context) => context.GetEndpoint()?.RequestDelegate is null;

Gross! It's actually pretty clever though. Is this anywhere in our real docs?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not in the docs, that's Damien's 7.0 change that broke this sample.

new EndpointMetadataCollection(metadata),
context.Request.Path);

Expand Down