Webrequest and shared files

G

Guest

Hi

I was wondering if it's possible to use the WebRequest class to access a file on windows shared folder with authentication? If yes, what would the syntax be? I've tried to look this up in the references available but to no avail

Also, is it safer (better practise) in an LAN environment to use HTTP requests to access shared files (via ASP.NET) rather than UNC file shares

TIA
Regards
John
 
W

William F. Robertson, Jr.

WebRequest request = WebRequest.Create( "http://server/file1.xls" );
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential( "user", "pass" );

request.GetResponse();

Now, this will work for integrated NT authentication. If you are using
forms authentication this won't help you.

I personally use UNC paths for getting files that way you can use the NTFS
security, plus I think it is a more efficient use of bandwidth transferring
files this way, as opposed to http.

HTH,

bill

John K. said:
Hi,

I was wondering if it's possible to use the WebRequest class to access a
file on windows shared folder with authentication? If yes, what would the
syntax be? I've tried to look this up in the references available but to no
avail.
Also, is it safer (better practise) in an LAN environment to use HTTP
requests to access shared files (via ASP.NET) rather than UNC file shares?
 
G

Guest

Thanks Bill

However, I think I'm missing the crucial bit here..

So are you saying that for a UNC share (e.g. "\\server\share\file.xml" ) the code would be the following

WebRequest request = WebRequest.Create( "http://server/share/file.xml" )
request.PreAuthenticate = true
request.Credentials = new NetworkCredential( "user", "pass" )

request.GetResponse()

I believe the syntax of the URI has to be different for the UNC share, surely? I've tried a couple of different variant
but usually get a logon denied related error, or that it can't find the network resource

Regards
Joh


----- William F. Robertson, Jr. wrote: ----

WebRequest request = WebRequest.Create( "http://server/file1.xls" )
request.PreAuthenticate = true
request.Credentials = new NetworkCredential( "user", "pass" )

request.GetResponse()

Now, this will work for integrated NT authentication. If you are usin
forms authentication this won't help you

I personally use UNC paths for getting files that way you can use the NTF
security, plus I think it is a more efficient use of bandwidth transferrin
files this way, as opposed to http

HTH

bil

John K. said:
file on windows shared folder with authentication? If yes, what would th
syntax be? I've tried to look this up in the references available but to n
avail
 
W

William F. Robertson, Jr.

I also was assuming you had a virtual directory set up called "share" that
points to \\server\share and has all the associated permissions.

Also, the URI you are using in your Create method, try placing that straight
into a browser and make sure you can open that way.

Can you not just access the file through UNC?

//You will also need to set the GetReponse to the WebResponse object.
WebResponse response = request.GetResponse();

//this probably isn't the only way to do this.
System.IO.Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader( stream );

string s = reader.ReadToEnd();

response.Close()

HTH,

bill

John K said:
Thanks Bill,

However, I think I'm missing the crucial bit here...

So are you saying that for a UNC share (e.g. "\\server\share\file.xml" )
the code would be the following:
WebRequest request = WebRequest.Create(
"http://server/share/file.xml" );
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential( "user", "pass" );

request.GetResponse();

I believe the syntax of the URI has to be different for the UNC share,
surely? I've tried a couple of different variants
 
G

Guest

Well, the current state of this application is using the setup that you assumed
(a virtual directory pointing to the unc share), but I was thinking that maybe it woul
be better to access the file directly rather than via the UNC share (hence my wonderin
if what the preferred best practise when choosing between these 2 options). If going directl
I would eliminate a potential fail-over point (e.g. the webserver going down, or being restarted etc...

From the Framework class library reference

"WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.

So there should be some way connect to a share \\server\share - but I haven't found the right syntax for it as yet
I can access the files by entering my unc share in the browser so I know the unc file path is correct

Joh

----- William F. Robertson, Jr. wrote: ----

I also was assuming you had a virtual directory set up called "share" tha
points to \\server\share and has all the associated permissions

Also, the URI you are using in your Create method, try placing that straigh
into a browser and make sure you can open that way

Can you not just access the file through UNC

//You will also need to set the GetReponse to the WebResponse object
WebResponse response = request.GetResponse()

//this probably isn't the only way to do this
System.IO.Stream stream = response.GetResponseStream()
StreamReader reader = new StreamReader( stream )

string s = reader.ReadToEnd()

response.Close(

HTH

bil

John K said:
Thanks Bill
"http://server/share/file.xml" )
request.PreAuthenticate = true
request.Credentials = new NetworkCredential( "user", "pass" )
surely? I've tried a couple of different variant
 
W

William F. Robertson, Jr.

If you can access the file through UNC I would recommend doing it that way.

This link has lots of examples:
http://msdn.microsoft.com/library/d...ef/html/frlrfSystemIOFileStreamClassTopic.asp

Here is some sample code to read the file into a byte array.

FileStream fileStream = new FileStream( @"\\server\share\file.xls",
FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader reader = new BinaryReader( fileStream );

byte [] bytes = reader.ReadBytes( ( int ) fileStream.Length );

bytes now holds all the content of the file.

bill


John K said:
Well, the current state of this application is using the setup that you assumed
(a virtual directory pointing to the unc share), but I was thinking that maybe it would
be better to access the file directly rather than via the UNC share (hence my wondering
if what the preferred best practise when choosing between these 2 options). If going directly
I would eliminate a potential fail-over point (e.g. the webserver going
down, or being restarted etc...)
From the Framework class library reference:

"WebRequest descendants are typically registered to handle a specific
protocol, such as HTTP or FTP, but can be registered to handle a request to
a specific server or path on a server."
So there should be some way connect to a share \\server\share - but I
haven't found the right syntax for it as yet.
 
W

William F. Robertson, Jr.

Here is some code that will work.

I created a virtual directory called temp that pointed to c:\temp on my hard
drive. (I did this at home so I didn't have a network path to test the vdir
with, but it shouldn't matter as long as permissions to read are granted.)

I required integrated nt authentication and turned off the anonymous access.
(probably something similiar to what you are doing.)

The file I opened was just a text file that I Response.Write() to the
browser, you would be able to parse the file, load into a dataset, or
literally stream the file back to the client if you want.

I hope this is what you were looking for.

HTH,

bill

WebRequest request = WebRequest.Create(
"http://localhost/tempasp/temp/trace.txt" );
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential("user", "pass", "domain" );

WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader( response.GetResponseStream() );

string line = String.Empty;
while ( ( line = reader.ReadLine() ) != null )
{
Response.Write( line );
Response.Write( "<BR>" );
}
reader.Close();
response.Close();



John K. said:
Thanks Bill,

I guess I will have to do something as what your're giving example of.

(Still slightly curious about if it's possible to do it via the Webrequest
class though - it would save me some small amount of code to rewrite.).
Cheers,
John

----- William F. Robertson, Jr. wrote: -----

If you can access the file through UNC I would recommend doing it that way.

This link has lots of examples:
http://msdn.microsoft.com/library/d...ef/html/frlrfSystemIOFileStreamClassTopic.asp

Here is some sample code to read the file into a byte array.

FileStream fileStream = new FileStream( @"\\server\share\file.xls",
FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader reader = new BinaryReader( fileStream );

byte [] bytes = reader.ReadBytes( ( int ) fileStream.Length );

bytes now holds all the content of the file.

bill


John K said:
Well, the current state of this application is using the setup that
you
assumed
(a virtual directory pointing to the unc share), but I was thinking
that
maybe it would
be better to access the file directly rather than via the UNC share
(hence
my wondering
if what the preferred best practise when choosing between these 2 options). If going directly
I would eliminate a potential fail-over point (e.g. the webserver
going
down, or being restarted etc...) specific
protocol, such as HTTP or FTP, but can be registered to handle a request to
a specific server or path on a server." but I
haven't found the right syntax for it as yet.
I can access the files by entering my unc share in the browser so I
know
the unc file path is correct. "share"
that
points to \\server\share and has all the associated permissions.
placing that
straight
into a browser and make sure you can open that way. object.
WebResponse response = request.GetResponse();
System.IO.Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader( stream ); "\\server\share\file.xml" )
the code would be the following:
"http://server/share/file.xml" ); share,
surely? I've tried a couple of different variants
find
the
network resource.
you
can
use the
NTFS bandwidth
transferring what would
the available but
to no file
 
C

Craig Burkett

Would you happen to know how I could provide the necessary credentials to
acces a UNC path with if I need to be able to access it with an account
other than the one that the user is logged in with? there's nothing in the
samples provided indicating how to do that.

Thanks In Advance,
Craig Burkett

William F. Robertson said:
If you can access the file through UNC I would recommend doing it that way.

This link has lots of examples:
http://msdn.microsoft.com/library/d...ef/html/frlrfSystemIOFileStreamClassTopic.asp

Here is some sample code to read the file into a byte array.

FileStream fileStream = new FileStream( @"\\server\share\file.xls",
FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader reader = new BinaryReader( fileStream );

byte [] bytes = reader.ReadBytes( ( int ) fileStream.Length );

bytes now holds all the content of the file.

bill


John K said:
Well, the current state of this application is using the setup that you assumed
(a virtual directory pointing to the unc share), but I was thinking that maybe it would
be better to access the file directly rather than via the UNC share
(hence
my wondering
if what the preferred best practise when choosing between these 2 options). If going directly
I would eliminate a potential fail-over point (e.g. the webserver going
down, or being restarted etc...)
From the Framework class library reference:

"WebRequest descendants are typically registered to handle a specific
protocol, such as HTTP or FTP, but can be registered to handle a request to
a specific server or path on a server."
So there should be some way connect to a share \\server\share - but I
haven't found the right syntax for it as yet.
I can access the files by entering my unc share in the browser so I know the unc file path is correct.

John

----- William F. Robertson, Jr. wrote: -----

I also was assuming you had a virtual directory set up called
"share"
that
points to \\server\share and has all the associated permissions.

Also, the URI you are using in your Create method, try placing that straight
into a browser and make sure you can open that way.

Can you not just access the file through UNC?

//You will also need to set the GetReponse to the WebResponse object.
WebResponse response = request.GetResponse();

//this probably isn't the only way to do this.
System.IO.Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader( stream );

string s = reader.ReadToEnd();

response.Close()

HTH,

bill

"\\server\share\file.xml" )
the code would be the following:
"http://server/share/file.xml" ); share,
surely? I've tried a couple of different variants
find
the
network resource.
you
can
use the
NTFS bandwidth
transferring what would
the available but
to no use
HTTP
UNC
 
C

Craig Burkett

Were you able to figure out as to how to programaticaly set up credentials
for an account other than the one the user is logged in with?

Thanks In Advance,
Craig Burkett


John K said:
Well, the current state of this application is using the setup that you assumed
(a virtual directory pointing to the unc share), but I was thinking that maybe it would
be better to access the file directly rather than via the UNC share (hence my wondering
if what the preferred best practise when choosing between these 2 options). If going directly
I would eliminate a potential fail-over point (e.g. the webserver going
down, or being restarted etc...)
From the Framework class library reference:

"WebRequest descendants are typically registered to handle a specific
protocol, such as HTTP or FTP, but can be registered to handle a request to
a specific server or path on a server."
So there should be some way connect to a share \\server\share - but I
haven't found the right syntax for it as yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top