【问题标题】:ASP.NET Core web.config location useASP.NET Core web.config 位置使用
【发布时间】:2020-12-11 19:17:24
【问题描述】:

我正在尝试保护文件夹中的几个 HTML 文件(请参阅我的问题 Protect Static Files with Authentication on ASP.NET CoreASP.NET Core authorization permission access folder with Identity Server)。我用 MVCRazor Pages 创建了两个项目,结果相同。此外,我还与 Identity Server 集成。我不能保护 HTML 文件。

然后,我的想法是使用web.config 只允许经过身份验证的用户访问该文件夹,例如:

<location path="html">
  <system.web>
    <authorization>
      <deny users ="*" />
    </authorization>
  </system.web>
</location>

然后我将它与我在 Azure 上部署的应用程序中找到的 web.config 合并。结果如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\PatientJourney.dll" 
                  stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
  <location path="infographics">
    <system.web>
      <authorization>
        <deny users ="*" />
      </authorization>
    </system.web>
  </location>
  <location path="html">
    <system.web>
      <authorization>
        <deny users ="*" />
      </authorization>
    </system.web>
  </location>
</configuration>

html文件夹是根目录下的物理文件夹,infographicsStartup.cs

中定义的虚拟文件夹

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "html")),
    RequestPath = "/infographics",
    OnPrepareResponse = ctx =>
    {
        if (ctx.Context.Request.Path.StartsWithSegments("/infographics"))
        {
            ctx.Context.Response.Headers.Add("Cache-Control", "no-store");

            if (!ctx.Context.User.Identity.IsAuthenticated)
            {
                // respond HTTP 401 Unauthorized with empty body.
                ctx.Context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                ctx.Context.Response.ContentLength = 0;
                ctx.Context.Response.Body = Stream.Null;

                // - or, redirect to another page. -
                // ctx.Context.Response.Redirect("/");
            }
        }
    }
});

尽管我尝试拒绝访问这两个文件夹(物理和虚拟),但每个用户都可以访问这些文件。然后,我的问题。

我可以将web.config 用于此安全文件夹还是不再受支持? 为什么locationweb.config 不起作用?有什么限制吗? 我想阻止未经身份验证的用户的 html 页面:还有其他想法吗?

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    如何管理用户使用中间件访问文件夹的权限?

    ASP.NET Core Middleware

    例如:

    public class RequestResponseLoggingMiddleware
    {
        private readonly RequestDelegate _next;
    
        public RequestResponseLoggingMiddleware(RequestDelegate next)
        {
            _next = next;
        }
    
        public async Task Invoke(HttpContext context, ILogger<RequestResponseLoggingMiddleware> logger, IConfiguration configuration, UserManager<User> userManager)
        {
            /*
            ...Managing users access to folders using IConfiguration and UserManager...
            */
            
            //Call the next delegate/middleware in the pipeline
            await _next(context);
        }
    }
    
    public static class RequestResponseExtensions
    {
        public static IApplicationBuilder UseRequestResponseLogging(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<RequestResponseLoggingMiddleware>();
        }
    }
    

    在您的 Startup.cs 中:

    app.UseRequestResponseLogging();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 2018-10-03
      相关资源
      最近更新 更多