【发布时间】:2020-12-11 19:17:24
【问题描述】:
我正在尝试保护文件夹中的几个 HTML 文件(请参阅我的问题 Protect Static Files with Authentication on ASP.NET Core 和 ASP.NET Core authorization permission access folder with Identity Server)。我用 MVC 和 Razor 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文件夹是根目录下的物理文件夹,infographics是Startup.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 用于此安全文件夹还是不再受支持?
为什么location 是web.config 不起作用?有什么限制吗?
我想阻止未经身份验证的用户的 html 页面:还有其他想法吗?
【问题讨论】:
标签: c# asp.net-core