I have a .net core MVC controller that is downloading a file from a mapped drive on a server. It uses NetworkCredential to pass the username and password for the drive:
[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());
}
}
this works fine when I run it locally in visual studio but when I put the code on the server it is trying to connect to the drive using the credentials of the box rather than those I am passing. Is there a way to force it to use the credentials?
After a lot of messing around I found a way to do this. If anyone else has this problem then this solution worked for me
https://www.c-sharpcorner.com/blogs/how-to-access-network-drive-using-c-sharp