c# - ASP.NET Core System.UnauthorizedAccessException : Access to the path 'C:\..." is denied

标签 c# asp.net-core asp.net-core-webapi

我是 ASP.NET Core 的新手,所以我正在尝试制作 API。我正在尝试通过我的 Web API 上传图片。图片将被保存到网络服务器上。我在 Windows Server 2016 Standard Edition VPS 机器上运行 API。我在机器上安装了 IIS 10。我返回的错误(远程调试时):

{System.UnauthorizedAccessException: Access to the path 'C:\HomeNET\Houses\13' is denied. at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode) at HomeNetAPI.Controllers.HouseController.d__6.MoveNext() in C:\Users\okuhl\Documents\HomeNet\Web API\HomeNetAPI\src\HomeNetAPI\Controllers\HouseController.cs:line 83}



这很奇怪,因为创建了目录,如果它们不存在。这是一些代码,失败发生的地方:
if (resultHouse != null)
{
    if (!Directory.Exists($"C:/HomeNET/Houses/{resultHouse.HouseID}"))
    {
        Directory.CreateDirectory($"C:/HomeNET/Houses/{resultHouse.HouseID}");
    }

    String fileName = new FileInfo(imageFile.FileName).Name;
    String fileExtension = new FileInfo(imageFile.FileName).Extension;
    using (var fileStream = new FileStream($"C:/HomeNET/Houses/{resultHouse.HouseID}", 
        FileMode.Create)) //Bombs out after this line
    {
        var result = imageFile.CopyToAsync(fileStream);
        if (result.IsCompleted)
        {
            newHouse.HouseImage = Path.Combine($"C:/HomeNET/Houses/{resultHouse.HouseID}", $".{fileExtension}");
            var updateResult = await Task.Run(() =>
            {
                return houseRepository.UpdateHouse(newHouse);
            });
            if (updateResult != null)
            {
                response.DidError = false;
                response.Message = $"House {newHouse.Name} has been created successfully! Invite friends ad family to your house!";
                response.Model = newHouse;
                return Ok(response);
            }
            else
            {
                response.DidError = true;
                response.Message = "Something went wrong with creating the house. Please try again";
                response.Model = newHouse;
                return BadRequest(response);
            }
        }
        else
        {
            response.DidError = true;
            response.Message = $"Something Went wrong with creating your house. House image could not be saved onto the system. \n {result.Exception.Message}";
            response.Model = newHouse;
            return BadRequest(response);
        }
    }
}

我试过让每个人都可以使用该文件夹,但没有用。我还尝试授予我的 AppPool 修改文件夹的权限,但没有奏效。

最佳答案

尝试检查正在运行应用程序池的用户(此处的说明:https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities)。

如果该用户无权访问 C 驱动器,则写入将失败。

(另外:尽量不要在你的代码中一次又一次地对路径进行编码。将 C:/HomeNET/Houses/ 声明为一个常量并以这种方式引用它,或者更好的是,从 web.config 中读取它)

关于c# - ASP.NET Core System.UnauthorizedAccessException : Access to the path 'C:\..." is denied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44512485/

相关文章:

c# - Autofixture - 新列表中基于先前构建的属性的属性

c# - 查看帖子表单返回null?

asp.net-core - 防止 Kestrel 在端口使用时崩溃

c# - 在 .NET Web API POST/PUT 方法中使用继承的类

azure - Application Insights 不捕获信息级别日志记录

c# - 使用 LINQ 的唯一项目列表

minOccurs ="0"元素上的 C# XSD 验证失败

c# - 在本地部署 Windows IoT 应用程序会出现应用程序激活错误

linux - Azure应用服务启动命令(必填)

c# - 如何读取 Response 主体并将其更改为 Controller 的覆盖 OnActionExecuted 方法