如何使用ASP.Net Core 2.0上的凭据访问UNC路径?

时间:2017-10-11 15:11:40

标签: asp.net-core .net-standard .net-standard-2.0

我正在尝试列出UNC文件夹上的文件

[HttpGet]
public IEnumerable<string> Get()
{            
    var list = Directory.GetFiles(@"\\files.mydomain.com\folder");

    return list;
}

但它正在给出这个例外

  

System.UnauthorizedAccessException:&#39;访问路径&#39; \\ files.mydomain.com \ folder&#39;被拒绝。&#39;

如何使用ASP.Net Core 2上的C#代码通知用户名和密码?

1 个答案:

答案 0 :(得分:-1)

试试这个。

NetworkCredential theNetworkCredential = new NetworkCredential(username, password, domain);

CredentialCache theNetCache = new CredentialCache();

theNetCache.Add(@"\\files.mydomain.com", theNetworkCredential, "Basic", theNetworkCredential);

然后做任何事情,比如获取文件夹列表:

string[] theFiles = System.IO.Directory.GetFiles(@"\\files.mydomain.com\folder");
相关问题