【发布时间】:2019-10-07 19:57:33
【问题描述】:
我想在我的 .net core 2.2 应用程序中获取某个文件作为字节数组。该应用程序使用 iis 并像这样启动:
new WebHostBuilder()
.UseKestrel(opt => opt.AddServerHeader = false)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>();
站点的应用程序池使用专用服务用户。此用户具有共享访问权限,并且位于 UNC-Path-Folder 和文件的安全列表中。
但是,当我尝试像这样读取文件时:
_fileContent = File.ReadAllBytes("\\working\UNC\Path\File.pdf");
我得到以下异常:
System.UnauthorizedAccessException: Access to the path '\\working\UNC\Path\File.pdf' is denied.
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.IO.File.ReadAllBytes(String path)
UNC 路径当前实际上是本地计算机上的一个目录。因此,我尝试使用 windows 路径 来加载文件,例如:“c:\Path\File.pdf”。这工作得很好!因此,我得出结论,该问题实际上与安全权限无关。
有人可以解释一下这个问题吗?
【问题讨论】:
-
异常显示“System.UnauthorizedAccessException”,因此它与安全相关。您应该授予 ApplicationPool 对 UNC 共享的身份读取权限。
标签: asp.net-core .net-core iis-8 asp.net-core-2.2 unc