Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get file to FileStream from remote path with another user credentials

In my application I use FileStream to read from a file, that is on the fileshare somewhere in the network. So my remoteFilePath variable is something like: \\computername\folder\file.pdf

FileStream fileStream = new FileStream(remoteFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 1024)

Unfortunately, the user that I'm running this application with (that I'm logged into the PC with) does not have access to this fileshare. I have another user (domain, login & password), that has access to those files.

Is it possible to use the other user credentials to get a file to filestream? Can I impersonate the user only to get a file, and then continue with my own user?

like image 905
Tschareck Avatar asked Aug 17 '12 08:08

Tschareck


People also ask

How do I retrieve a file from FileStream?

Read file using FileStreamFirst create FileStream to open a file for reading. Then call FileStream. Read in a loop until the whole file is read. Finally close the stream.

How does FileStream work C#?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

Which method sets the position in the current FileStream?

FileStream.Seek(Int64, SeekOrigin) Method (System.IO) Sets the current position of this stream to the given value.


2 Answers

Thank you for your answers. Since the share was in another domain, it was not so easy to impersonate.

I found another, easier solution. I mapped a network drive, and checked the option Connect using different credentials. Then I connect to this drive instead of the remote path.

string mappedFilePath = filePath.Replace(@"\\computername\", @"Y:\")

and use this new string in FileStream constructor.

like image 150
Tschareck Avatar answered Sep 29 '22 03:09

Tschareck


You should use impersonation. More info at http://msdn.microsoft.com/en-us/library/w070t6ka.aspx

like image 26
Cybermaxs Avatar answered Sep 29 '22 03:09

Cybermaxs