如何将 Razor Page 静态资产映射到自定义文件夹并需要身份验证

问题描述 投票:0回答:1

我有一个使用 Razor Pages 的 .NET 6.0 Web 应用程序,它使用默认的 wwwroot 作为 Web 根目录来服务器静态资产。正如预期的那样,这些文件可以公开访问。

该应用程序使用标准页面结构如下:

  • 我的网络应用程序
    • 自定义根目录
    • wwwroot
    • 页数
      • 包含文件夹1
        • PageOne.cshtml
        • PageOne.cshtml.cs
        • PageOne.cshtml.css
        • PageOne.cshtml.js
      • 包含文件夹2
        • PageTwo.cshtml
        • PageTwo.cshtml.cs
        • PageTwo.cshtml.css
        • PageTwo.cshtml.js
        • 第三页.cshtml
        • PageThree.cshtml.cs
        • PageThree.cshtml.css
        • PageThree.cshtml.js

发布应用时,静态资产发布在wwwroot下:

  • CSS 文件通过 CSS 隔离组合并存储在 wwwroot/MyWebApp.styles.css
  • JS文件存储在wwwroot/Pages/ContainingFolderX/PageName.cshtml.js

我的客户要求保护“敏感”内容(即调用具有端点信息的 JS 文件),以便它们不公开。即使这些端点已知,我们的应用程序也是安全的,但目标是掩盖这些信息。

基于这个需求,我想做两件事:

  1. 将静态资源发布到自定义文件夹(例如上例中的“custom-root”,与 wwwroot 处于同一目录级别)。理想情况下,我只需要移动 JS 文件,但如果有必要,我愿意移动所有文件。 (我不知道如何实现这一点,现在我一直在部署后复制它们以进行测试)
  2. 将身份验证中间件添加到管道后,添加第二个(或多个,如果需要)静态文件配置。

我测试了将“app.UseStaticFiles();”行移到了“app.UseAuthentication();”行之后,以验证我的意图。此操作成功,除了现在我未经身份验证的页面无法访问 CSS 和 JS 内容(如预期) ).

然后,我尝试将第二个静态文件选项添加到管道中,如 Microsoft 文档中指定的那样(https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view= aspnetcore-6.0)但没有运气:

public static class Program
{
    var builder = WebApplication.CreateBuilder(args);

    ...

    var app = builder.Build();

    ...

    app.UseHttpsRedirection();

    app.UseStaticFiles(); // serve public files from wwwroot

    app.UseRouting();

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "custom-root")),
        RequestPath = new PathString("/Pages"),
    }); // serve files that require authentication

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapRazorPages();
    });

    app.UseRequestLocalization("en-US");

    app.Run();
}

我期望的是 UseStaticFiles 的第二个实例将捕获以下格式的请求并从我的自定义位置为它们提供服务。

https://www.mywebsite.com/Pages/ContainingFolder1/PageOne.js

但是,我收到的是指向我的自定义“PageNotFound”endopint 的 302 重定向(大概是因为它仍在检查 wwwroot 上的内容而没有找到它,那么我的自定义 404 中间件将重定向到我的未找到页面)。

我不知道下一步该尝试什么。

asp.net-core .net-6.0 razor-pages static-files
1个回答
0
投票

您可以尝试分支中间件管道以有条件地使用静态文件:

app.UseWhen(context => !context.Request.Path.StartsWithSegments("/pages"), //determine if not start with "/pages" 
    appBuilder =>
    {
        appBuilder.UseStaticFiles();
    });

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "custom-root")),
    RequestPath = new PathString("/Pages"),
}); // serve files that require authentication

请注意,

UseWhen
将重新加入主管道,而
Mapwhen
则不会。
参考:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0#branch-the-middleware-pipeline

© www.soinside.com 2019 - 2024. All rights reserved.