如何使用 IIS 和 ASP.NET Core 从另一台服务器获取文件

How to get files from another server with IIS and ASP.NET Core

我们目前在文件下载方面遇到了问题。 在我们的设置中,我们有 2 个服务器。 Server-1 是托管具有 ASP.NET Core Identity 的 ASP.NET Core 应用程序的服务器。 另一台服务器 Server-2 包含我们要下载的文件,并且位于我们(必须)通过 VPN 连接访问的另一个域中。

当我在本地测试它时,它运行得非常好。尽管将其发布到 Server-1 后下载停止工作。路径是正确的,如果我尝试将文件路径粘贴到资源管理器中,PDF 将被打开。

我在应用程序和符号链接中尝试使用 UNC 路径,文件系统中的快捷方式以及网络驱动器。遗憾的是,没有任何效果。

我在网上找到了一些相关的问题,但大部分都和我们的不一样。那些相似的要么没用,要么问题没解决。

这里是下载功能的简短预览。遗憾的是,我无法让您访问我们的服务器进行测试。

public ActionResult Download(string filename)
        {
            try
            {
                string filePath = _path + filename;                

                if (!System.IO.File.Exists(filePath))
                {
                    //Logging

                    return View("Close");
                }

                byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);

                return File(fileBytes, "application/force-download", filename);
            }
            catch (Exception e)
            {
                // Logging

                return View("Close");
            }            
        }

没有抛出异常,它只是说文件不存在(但确实存在)。
编辑: 因为检查文件是否存在,所以没有抛出异常。这是例外。

System.IO.IOException: Wrong username or password. : '{PATH_TO_FILE}'
   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.File.ReadAllBytes(String path)
   at Project.Web.Controllers.InvoiceController.Download(String filename) in C:\BuildAgents\work\{hash}\Project\Project.Web\Controllers\InvoiceController.cs:line 78

看到异常的最后一行非常有趣,该路径位于我们的服务器上,TeamCity 运行 正在发布(又是另一个服务器,Server-0)?

如前所述:两台服务器位于不同的域中,Server-1 可以通过 VPN 访问 Server-2。

示例:server1domain\webuser -> VPN -> server2domain\user
这在本地运行良好(使用调试器)

我读到它与国际空间站本身有关。有什么想法(为了理解)以及如何解决这个问题吗?

提前致谢!

编辑 2:
我实施了这个解决方案并且效果很好! https://www.c-sharpcorner.com/blogs/how-to-access-network-drive-using-c-sharp
我在 Whosebug 的某处看到了原始解决方案。会post吗,如果我再找到它。

我实施了这个解决方案并且效果很好! https://www.c-sharpcorner.com/blogs/how-to-access-network-drive-using-c-sharp 我在 Whosebug 的某处看到了原始解决方案。如果我再次找到它,我会 post 它。