【问题标题】:How to get files from another server with IIS and ASP.NET Core如何使用 IIS 和 ASP.NET Core 从另一台服务器获取文件
【发布时间】:2021-11-01 16:27:04
【问题描述】:

我们目前在处理文件下载时遇到问题。 在我们的设置中,我们有 2 台服务器。 Server-1 是托管具有 ASP.NET Core 标识的 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\1\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
我在stackoverflow的某个地方看到了原始解决方案。如果我再找到它会发布它。

【问题讨论】:

  • 您可以发布您编辑的内容作为答案,它将帮助更多的论坛用户。

标签: asp.net-mvc asp.net-core iis asp.net-web-api network-programming


【解决方案1】:

我实施了这个解决方案,它运行良好! https://www.c-sharpcorner.com/blogs/how-to-access-network-drive-using-c-sharp 我在stackoverflow的某个地方看到了原始解决方案。如果我再次找到它,我会发布它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-12
    • 2021-03-05
    • 2017-01-23
    • 2021-10-04
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    相关资源
    最近更新 更多