By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

Webrequest and shared files

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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." <ts*****@hotmail.com> wrote in message
news:BD**********************************@microsof t.com...
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

Nov 18 '05 #2

P: n/a
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." <ts*****@hotmail.com> wrote in messag
news:BD**********************************@microsof t.com..
Hi
I was wondering if it's possible to use the WebRequest class to access 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 Also, is it safer (better practise) in an LAN environment to use HTT requests to access shared files (via ASP.NET) rather than UNC file shares TIA

Regards
Joh


Nov 18 '05 #3

P: n/a
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" <ts*****@hotmail.com> wrote in message
news:E6**********************************@microsof t.com...
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 but usually get a logon denied related error, or that it can't find the network resource.
Regards,
John
----- 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 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." <ts*****@hotmail.com> wrote in message
news:BD**********************************@microsof t.com...
> 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


Nov 18 '05 #4

P: n/a
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" <ts*****@hotmail.com> wrote in messag
news:E6**********************************@microsof t.com..
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 th 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 ar usin forms authentication this won't help you
I personally use UNC paths for getting files that way you can use th
NTF security, plus I think it is a more efficient use of bandwidt transferrin files this way, as opposed to http
HTH
bil
"John K." <ts*****@hotmail.com> wrote in messag news:BD**********************************@microsof t.com..
Hi
I was wondering if it's possible to use the WebRequest class t access file on windows shared folder with authentication? If yes, what woul th syntax be? I've tried to look this up in the references available bu to n avail Also, is it safer (better practise) in an LAN environment to us
HTT requests to access shared files (via ASP.NET) rather than UNC fil

shares TIA

Regards
Joh

Nov 18 '05 #5

P: n/a
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/de...ClassTopic.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" <ts*****@hotmail.com> wrote in message
news:B8**********************************@microsof t.com...
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

"John K" <ts*****@hotmail.com> wrote in message
news:E6**********************************@microsof t.com...
> 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
> but usually get a logon denied related error, or that it can't find
the network resource. >> Regards, > John
>>> ----- 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 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." <ts*****@hotmail.com> wrote in message

> news:BD**********************************@microsof t.com...
>> 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
>>>

Nov 18 '05 #6

P: n/a
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." <ts*****@hotmail.com> wrote in message
news:0A**********************************@microsof t.com...
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/de...ClassTopic.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" <ts*****@hotmail.com> wrote in message
news:B8**********************************@microsof t.com...
> 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
>> "John K" <ts*****@hotmail.com> wrote in message

> news:E6**********************************@microsof t.com...
>> 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
>> but usually get a logon denied related error, or that it can't find the
> network resource.
>>> Regards,
>> John
>>>> ----- 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
> 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." <ts*****@hotmail.com> wrote in message
>> news:BD**********************************@microsof t.com...
>>> 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
>>>>

Nov 18 '05 #7

P: n/a
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, Jr." <wf*********@kpmg.com> wrote in message
news:Ov*************@tk2msftngp13.phx.gbl...
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/de...ClassTopic.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" <ts*****@hotmail.com> wrote in message
news:B8**********************************@microsof t.com...
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

"John K" <ts*****@hotmail.com> wrote in message
news:E6**********************************@microsof t.com...
> 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
> but usually get a logon denied related error, or that it can't find the
network resource.
>> Regards,
> John
>>> ----- 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
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." <ts*****@hotmail.com> wrote in message
> news:BD**********************************@microsof t.com...
>> 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
>>>



Nov 18 '05 #8

P: n/a
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" <ts*****@hotmail.com> wrote in message
news:B8**********************************@microsof t.com...
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

"John K" <ts*****@hotmail.com> wrote in message
news:E6**********************************@microsof t.com...
> 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
> but usually get a logon denied related error, or that it can't find
the network resource. >> Regards, > John
>>> ----- 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 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." <ts*****@hotmail.com> wrote in message

> news:BD**********************************@microsof t.com...
>> 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
>>>


Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.