Skip to content

Commit 5f91f1b

Browse files
authored
Fix endpoints in StaticFileAuth sample #43274 (#43352)
1 parent 49599d2 commit 5f91f1b

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

src/Security/samples/StaticFilesAuth/Startup.cs

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
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;
5-
using System.Collections.Generic;
6-
using System.IO;
7-
using System.Linq;
84
using Microsoft.AspNetCore.Authentication.Cookies;
95
using Microsoft.AspNetCore.Authorization;
10-
using Microsoft.AspNetCore.Builder;
11-
using Microsoft.AspNetCore.Hosting;
12-
using Microsoft.AspNetCore.Http;
13-
using Microsoft.AspNetCore.Mvc;
14-
using Microsoft.Extensions.Configuration;
15-
using Microsoft.Extensions.DependencyInjection;
166
using Microsoft.Extensions.FileProviders;
17-
using Microsoft.Extensions.Hosting;
187

198
namespace StaticFilesAuth;
209

@@ -53,7 +42,7 @@ public void ConfigureServices(IServiceCollection services)
5342
{
5443
return false;
5544
}
56-
if (context.Resource is Endpoint endpoint)
45+
if (context.Resource is HttpContext httpContext && httpContext.GetEndpoint() is Endpoint endpoint)
5746
{
5847
var userPath = Path.Combine(usersPath, userName);
5948

@@ -134,12 +123,14 @@ private static void SetFileEndpoint(HttpContext context, PhysicalFileProvider fi
134123
var fileSystemPath = GetFileSystemPath(files, context.Request.Path);
135124
if (fileSystemPath != null)
136125
{
137-
var metadata = new List<object>();
138-
metadata.Add(new DirectoryInfo(Path.GetDirectoryName(fileSystemPath)));
139-
metadata.Add(new AuthorizeAttribute(policy));
126+
var metadata = new List<object>
127+
{
128+
new DirectoryInfo(Path.GetDirectoryName(fileSystemPath)),
129+
new AuthorizeAttribute(policy)
130+
};
140131

141132
var endpoint = new Endpoint(
142-
c => throw new InvalidOperationException("Static file middleware should return file request."),
133+
requestDelegate: null,
143134
new EndpointMetadataCollection(metadata),
144135
context.Request.Path);
145136

0 commit comments

Comments
 (0)