Roman Suska Roman Suska - 1 year ago 126
ASP.NET (C#) Question

Using WebClient to get a intranet files

In our I have company intranet a server, that is responsible for storing files. Initially, the server had to operate only in an intranet environment, but now there is a need to share files with external web applications. Making this server accessible from the internet is not an option.

I want to create a ASP.NET MVC solution that uses the WebClient to get these files from the intranet server and send back them to the user through FileResult of the external app. This client would be provided with custom domain user credentials. So far I have tried to create a CredentialCache class, set correct credentials and append it to WebClients Credentials property like in the following code:

public ActionResult Download(int id, string fileName)
var fileService = new FilesService();
var documentUrl = fileService.GetUrlFileByFileId(id);
string filePath = "" + documentUrl;
var fileNameFromUrl = filePath.Substring(filePath.LastIndexOf("\\") + 1);
byte[] filedata;

CredentialCache cc = new CredentialCache();
cc.Add(new Uri(""),
new NetworkCredential("myUserName", "myPassword", "myDomain"));

using (var client = new WebClient())
client.Credentials = cc;
filedata = client.DownloadData(filePath);

string contentType = MimeMapping.GetMimeMapping(filePath);

var cd = new ContentDisposition
FileName = fileName,
Inline = false

Response.AppendHeader("Content-Disposition", cd.ToString());

return File(filedata, contentType);

According to the question posted in Domain credentials for a WebClient class don't work it should work, but it’s not. It’s running only if I run the problem on localhost, but when I publish my solution on a test server, it return 401 error. My question is did how to get this working? And is it possible to download files through this method?

UPDATE--- I've published my test app on another server and it started to working. Now the test app is on another server than the server That stores files. Any ideas why it's not working when both are on the same machine?

Answer Source

Ok, I found the solution on this site:

The solution was to add an registry entry and add my web apps to this entry to allow back connections.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download