c# - 下载文件抛出 UnauthorizedAccessException net core 2

标签 c# iis asp.net-core-2.0 file-permissions

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

这是一个由 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“开发”

但没有成功。

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

在网络 Controller 中,到目前为止一切都很顺利

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://learn.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities ,您应该在 cmd 中运行以下命令:

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

关于c# - 下载文件抛出 UnauthorizedAccessException net core 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50340147/

相关文章:

c# - 通用 TryParse

c# - ODP.NET : Parameter Types "cached" on identical CommandText

c# - 从动态添加的文本框获取值 asp.net c#

c# - ConcurrentQueue 和 Parallel.ForEach

ASP.NET Core - 应用程序在发布后未连接到数据库

.net - 已找到应用程序依赖项 list (Test.deps.json) 中指定的程序集,但具有不同的文件扩展名 :

asp.net - 如何判断 IIS 应用程序池被回收的原因

azure - 我应该如何在 ASP.NET Core 2.0 API 中使用 HttpClient

dictionary - 为什么 SignalR 在序列化字典并将其发送给客户端时将字典键的第一个字母转换为小写?

asp.net-mvc - 不使用身份的外部登录 asp.net core 2.0