0

There's a file on my network - we'll say it's sitting at the following location:

server-name/folder1/folder2/file.xlsx

In my webapp, I'm trying to download this file when a user clicks on a link.

If I run the webapp locally, it works fine. When I run it off of my hosting server, however, it gives me an error:

System.IO.IOException: Logon failure: unknown user name or bad password.

I'm currently using the following code to try and attempt this:

byte[] fileBytes = System.IO.File.ReadAllBytes(@"server-name\folder1\folder2\file.xlsx");
return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

If I log into the hosting server (using my personal Windows credentials), I'm able to access this location via File Explorer, so I know that the connection between the two is fine.

I've seen solutions related to impersonation, but from what I understand, that would require a central username/password. I just want the method that utilizes their own windows credentials.

Kulahan
  • 512
  • 1
  • 9
  • 23
  • Are you hosting this on IIS? You can use Windows Authentication and impersonate the user without the username/password (just use the current `HttpContent.User`). See also https://stackoverflow.com/q/1405612/215552 – Heretic Monkey Sep 21 '18 at 18:20

1 Answers1

0

The account that the hosting server runs as has no access to the file, which is why you get the error message.

Possible fixes are:

  • Change the file security to allow access by the account the web server runs as.

  • Change the account the web server runs as to a different account that has access to the file(s)

  • Have your code impersonate a user that has access to the files.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32