views:

523

answers:

3

I'm attempting to send a file to the browser that is located on a remote host (via UNC).

The web app is running both under impersonation and an App Pool with a user that has full access to the remote host's UNC directory (via matching credentials).

I can read and write files with no issues on the UNC path using the System.IO object's File.Copy. However, when I attempt

  Dim file As System.IO.FileInfo = New System.IO.FileInfo([UNCFilePath])
  Response.Clear()
  Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name.Replace(" ", "_"))
  Response.AddHeader("Content-Length", file.Length.ToString())
  Response.ContentType = "application/octet-stream"
  Response.WriteFile(file.FullName)

I get

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

If I copy the file via File.CopyTo to a local directory and do a writefile from there, it works.

My main question, outside of solution to this problem, is why does File.Copy work and File.Response not work? Aren't they both using System.IO?

+1  A: 

I recommend enabling security event logging on the target box. It's not always clear which accounts play a part in communications. In this case, I wouldn't be surprised if the ASPNET machine account came into the picture, even though you are using impersonation.

To enable security event logging in Windows XP Pro:

  1. Go to Control Panel -> Administrative Tools -> Local Security Policy
  2. Expand Local Policies and view the Audit Policy folder
  3. Double click on any of the items here to indicate what needs to be logged

You can view the Security event log by going to the Control Panel -> Administrative Tools group and double clicking on the Event Viewer icon.

David Andres
A: 

Thanks for the help David. This showed me that the App Pool was actually NOT running under the proper credentials.

I guess the lesson to be learned is that ASP Impersonation (web.config) is not used when accessing a remote UNC file via Response.WriteFile. Instead it uses the Application Pool identity.

Matias Nino
A: 

i have a code that works in win server 2003 + iis6 but not in new servers iis7.5.

i was able to write, list folders, but not download, when i do that i retrieve "access denied" error.

I solved problem changing

Response.WriteFile(f.FullName));

in

Response.BinaryWrite(File.ReadAllBytes(f.FullName));