下载文件抛出 UnauthorizedAccessException net core 2

Downloading a file throws UnauthorizedAccessException net core 2

我正在尝试下载文件。 (它之前是工作的)但现在它开始失败了。

这是一个由 web api net core 2 项目支持的 .net core 2 web 项目。它抛出以下错误消息。

Error. An error occurred while processing your request. Request ID: 0HLDPTBLNENOG:0000000A

Development Mode Swapping to Development environment will display more detailed information about the error that occurred.

Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application.

首先,我不知道如何启用开发模式,我什至尝试用这个命令更改环境变量

setx ASPNETCORE_ENVIRONMENT "development"

但是没用。

无论如何,我首先尝试记录该过程。我在 webapi 中有一种方法可以在存储库中定位 de 文件,并将文件位置 returns 定位到 web 控制器。根据我的日志,它没有抛出异常,一切都按计划进行。

在网络控制器中,到此为止一切正常

return File(new FileStream(location, FileMode.Open), "application/x-msdownload", filename.doc);

它抛出以下异常的地方(这是异常消息中提到的第 94 行)。

:System.UnauthorizedAccessException: Access to the path 'I:\Repository\Filename.doc' is denied.
at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at AbAWeb.Controllers.DocumentController.Download(String docId) in C:\SOME FOLDERS\Controllers\DocumentController.cs:line 94

所以我认为它与权限有关。

我添加了 IUSR、IIS_IUSRS、NETWORK SERVICE,我什至尝试了 Everyone,它仍然抛出相同的异常。

一些帖子说我必须添加 IIS Apppool 用户,但我在检查名称对话框中找不到它们。

文件在硬盘I:网站在C:.

谢谢

根据 https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities,您应该 运行 命令中的以下内容:

ICACLS <Folder Name> /grant "IIS AppPool\DefaultAppPool:F" /t

我必须将 IIS_IUSRS 添加到应用程序文件夹才能解决此问题。授予写入和读取权限,以便上传和下载都可以工作。