gpt4 book ai didi

c# - .net core 2.0 mvc + kestrel - 如何将目录锁定到 Controller 操作?

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:18 26 4
gpt4 key购买 nike

我的核心 2.0 应用设置了身份验证角色,我的 Controller 操作设置为[Authorize(Roles="demo_user")]

授权工作正常,我的问题是如何锁定内容目录并仅通过 Controller 操作为它们提供服务?

阅读一些文档,看起来我需要根据这篇文章提供文档: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files

我相信我感兴趣的信息是这一段:

Static file authorization The static file module provides no authorization checks. Any files served by it, including those under wwwroot are publicly available. To serve files based on authorization: Store them outside of wwwroot and any directory accessible to the static file middleware and Serve them through a controller action, returning a FileResult where authorization is applied

有没有人有在 wwwroot 之外存储目录并通过 Controller 操作提供它们的示例?

最佳答案

要通过 ASP.NET Core 中的操作提供文件,您可以执行以下操作:

[Authorize]
public IActionResult GetMyFile()
{
byte[] fileBytes = System.IO.File.ReadAllBytes("MyPrivateFiles/file1.txt");
return new FileContentResult(fileBytes, "text/plain");
}

请记住,您的应用程序需要对 MyPrivateFiles 文件夹具有读取权限。

关于c# - .net core 2.0 mvc + kestrel - 如何将目录锁定到 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326840/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com