赞!最终就是按照这个思路实现的
Startup.Configure() 中的代码:
app.UseDefaultFiles();
app.UseCnblogsCookieAuthentication();
app.UseWhen(
c => c.Request.Path.Value == "/index.html",
_ => _.UseMiddleware());
app.UseStaticFiles();
AuthorizeStaticFilesMiddleware 的实现代码:
public class AuthorizeStaticFilesMiddleware
{
private readonly RequestDelegate _next;
public AuthorizeStaticFilesMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context, IAuthorizationService authorService)
{
if(!context.User.Identity.IsAuthenticated)
{
await context.ChallengeAsync();
return;
}
var result = await authorService.AuthorizeAsync(context.User, Startup.POLICY_NAME);
if (!result.Succeeded)
{
await context.ForbidAsync();
}
else
{
await _next(context);
}
}
}