3

I want to read files off of different windows machine on the same network, NOT part of the same domain though. (ASP.NET C# app)
Tried FileStream (can't authenticate), tried FileWebRequest (reverts to FileStream when file:/// is used), and impersonation (support.microsoft.com/kb/306158#4) Which says "impersonation failed" on my Vista.
Update: I've fixed "impersonation failed" issue. But still get "Access denied" from the other machine, even though i have "mirrored" user on both, so the question remains...
What is the right way to approach this task?

Yakov S
  • 31
  • 1
  • 5
  • Why not write a simple service to serve the files? You can add authentication etc as required. IMHO, trying to directly access the file via a network share or something similar is just asking for things to go wrong. – Gregory Oct 17 '09 at 12:14

4 Answers4

1

Probably not the answer you're looking for, but you asked for the right way, so... join the file server to the domain and grant ASP.NET's service account access to the share.

Pawel Marciniak
  • 2,208
  • 14
  • 17
0

I'm not sure if this is proper approach, but it's the way I do it. Assuming that you know a login for that computer, I P/Invoke a WNetConnection to the IPC$ object to authenticate with the machine, do my work, then do another P/Invoke to remove the connection to the IPC$.

ewrankin
  • 253
  • 1
  • 7
0

Try WebClient. You can use this to access a URI and I believe this includes using the 'file' protocol (or you could publish the remote file via HTTP).

You can use WebClient for a traditional download:

WebClient.DownloadFile(Uri address, string localFile)

Or return a stream:

WebClient.OpenRead(Uri address)

However your authentication issues may still remain. You can provide credentials in a standard Uri manner though may not be possible using the 'file' protocol.

I have never tried it, but I believe you can specify impersonation in the web.config

<authentication mode="Windows" />
<identity impersonate="true" username="[user]" password="[password]"/>

Though this sounds like a security risk, making your application take on the credentials of a network user rather than the anonymous user default.

More here anyway: How To: Use Impersonation and Delegation in ASP.NET 2.0

tjmoore
  • 1,045
  • 1
  • 8
  • 19
0

Map a drive e.g. "Z:" to the remote machine on the machine where your code is running. The access the file using Z:...

saille
  • 9,014
  • 5
  • 45
  • 57