IIS 上映射驱动器的 NetworkCredential 不起作用

问题描述

我有一个 .net 核心 MVC 控制器,它从服务器上的映射驱动器下载文件。它使用 NetworkCredential 来传递驱动器的用户名和密码:

[HttpGet("[Action]")]
public IActionResult PdfContract(string fileName)
{
  NetworkCredential theNetworkCredential = new NetworkCredential(_contractsUsername,_contractsPassword);
        
  CredentialCache theNetCache = new CredentialCache();
  theNetCache.Add(new Uri(_contractsPath),"Basic",theNetworkCredential);

    try
      {
        var path = _contractsPath + @"\" + fileName;
            
        if (System.IO.File.Exists(path))
          {
            return new PhysicalFileResult(path,"application/pdf");
          }
          else
          {
            return NotFound(path);
          }
        }
        catch (Exception exception)
        {
            return NotFound(exception.ToString());
        }
    }

当我在 Visual Studio 本地运行它时,这工作正常,但是当我将代码放在服务器上时,它试图使用框的凭据而不是我传递的凭据连接到驱动器。有没有办法强制它使用凭据?

解决方法

经过一番折腾,我找到了一种方法来做到这一点。如果其他人有这个问题,那么这个解决方案对我有用

https://www.c-sharpcorner.com/blogs/how-to-access-network-drive-using-c-sharp