。Net Core 2.2 File.ReadAllBytes(fileName)抛出UNC路径的System.UnauthorizedAccessException

问题描述 投票:0回答:1

我想在.net core 2.2应用程序中获取某个文件作为字节数组。该应用程序使用iis并按以下方式启动:

new WebHostBuilder()
                .UseKestrel(opt => opt.AddServerHeader = false)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>();

该站点的应用程序池使用专门的服务用户。该用户具有对共享的访问权限,并且在UNC路径文件夹和直接文件的安全性列表中。

但是,当我尝试像这样读取文件时:

_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”。这工作正常!因此,我得出结论,该问题实际上与安全权限无关。

有人可以阐明这个问题吗?

asp.net-core .net-core iis-8 asp.net-core-2.2 unc
1个回答
0
投票

感谢UNOPARATOR的评论,我发现了问题。由于直接使用Windows路径访问文件是可行的,因此它本身的安全性就不会成为问题,但是由于通过unc路径进行访问会产生例外,因此实际上很明显网络共享的安全性设置存在问题。确实,在再次分析共享之后,我注意到我没有在共享的根文件夹上检查共享设置,而是在共享内的子文件夹上进行了检查。我对根目录共享授予读取权限后,一切正常!

© www.soinside.com 2019 - 2024. All rights reserved.