shakalama

hi,

i'm learning network programming i use my localhost to do this, i got exception "(401) unauthorized ", is this a problem in my code or IIS , i had changed the access permission for this folder "ecommerce" to read and write but i still get this exceptin. is there any suggestion to avoid this

here its my code

private void WriteToServer_Click(object sender, EventArgs e)

{

try

{

//new client

WebClient client = new WebClient();

//open stream to write to server

Stream strm = client.OpenWrite("http://localhost/ecommerce/newfile.txt", "PUT");

StreamWriter writer = new StreamWriter(strm);

writer.WriteLine("Hello World!");

writer.Close();

}

catch (WebException ex)

{

MessageBox.Show(ex.Message);

}

}

thx in advance




Re: .NET Framework Networking and Communication WebClient.OpenWrite()

Jorg Jooss - MSFT

IIS does not allow PUT requests by default. Run the IIS admin applet, and open the properties of the "ecommerce" directory. On the "Directory" tab, grant "Write" permission.

Note that this still requires the executing process to have actual NTFS write permissions for the ecommerce directory.





Re: .NET Framework Networking and Communication WebClient.OpenWrite()

shakalama

hi,

many thx for your respond

i had allready grand write permission , but what do you mean by executing process sorry i'm novice in this

Best Regards






Re: .NET Framework Networking and Communication WebClient.OpenWrite()

shakalama

hi,

ok let me rephrase my question i'm learning C# and i found this part in a book that i read now

"

There is also an OpenWrite() method in the WebClient class. This method returns a writeable stream for you to send data to a URI. You can also specify the method used to send the data to the host; the default method is POST. The following code snippet assumes a writeable directory named ecommerce on the local machine. The code will create a file in the directory with the name newfile.txt and the contents

¡°Hello World¡±.

"

so the question is how to make a writeable direcotry in my local machine

i will also edit this thread subject from webclient.open write to "how to make a writeable direcotry in my local machine to test by it"

thx in advance






Re: .NET Framework Networking and Communication WebClient.OpenWrite()

Jorg Jooss - MSFT

To answer your first question¡ªlike any process, IIS executes in the context of a Windows user account, and is limited to whatever this account is priviliged to do. A PUT request without authentification is executed as the anonymous user (IUSR_<machinename>).* In order to PUT a file in a directory served by IIS, IUSR_<machinename> requires NTFS write permissions on the physical directory.

But I've just realized that this seems to be the case by default. So once you've enabled PUT requests as mentioned before, the code should work right away (it works for me at least ).

* This assumes you didn't change the default configuration for anonymous users.





Re: .NET Framework Networking and Communication WebClient.OpenWrite()

shakalama

hi, Jorg

sounds you miss understood me you talk about things as if i know how to do this, all my experience with IIS is that Asp pages need to setup IIS from control panel nothing more than that, and now i know new thing is how to tick "write" in directory tab

so if you don't mind tell me how to do what you are saying (NTFS wirting permission) or at least give me a direction where to find the solution

also i would like to ask about a good material to learn network programing

thx in advance






Re: .NET Framework Networking and Communication WebClient.OpenWrite()

Jorg Jooss - MSFT

In Windows Explorer, right click the file or directory for which you want to change permissions, and select "Properties" and open the "Security" tab. Here you can assign detailed permissions like read, write, execute etc to any group or user.

If you don't see a Security tab, you first have to open Windows Explorer, select Tools->Folder Options¡­ On the "View" tab, make sure the option "Use simple file sharing (Recommded)" is not selected (that's one of the dumbest recommendations you'll ever find).

There are a few books on .NET network programming out there. I suggest you go to your favorite online book store, search for ".net network programming", and see what other people liked. Mind also the "network" in network programming¡ªif you have little or no idea of TCP/IP networking, you might want to start with a primer on networking before you start programming.





Re: .NET Framework Networking and Communication WebClient.OpenWrite()

shakalama

hi, Jorg

man that was very cool , many many many many many many thx

i got a book that starting by history since unix sockets and tcp, udp history

many thx again Jorg

i did those steps

/* ecommerce folder must have ntfs writting permission through those steps

* 1) allow securty tab to appear in folder properties by doing

* windows explorer >> tools menue >> folder option >> view >> remove "use simply file sharing(recommended)"

* 2) right click ecommerce folder >> properties >> security tab >> give write permission to Iuser

* 3) open IIS edit my folder authentication by

* start >> run >> inetmgr >> select ecommercefolder >> properties >> directory tab >> tick "Write"

* in IIS myfolder properties >> go to Directory security tab >> anonymous access edit >> tick anonmous access

* */

best regards






Re: .NET Framework Networking and Communication WebClient.OpenWrite()

peterdarvas

Hello Jorg

This had me stumped for days.

I had set Write permissions in IIS; allowed anonymous access; checked and double checked Users, Groups etc.

I scrutinized Web Developer debug messages & fields but these yielded no clues. The system log file did report an Authentication error.

I had unchecked the Tools=>Folder Options=>View->Use simple file sharing(Recommended) option.

But I still kept getting 401/403 errors

It was your advice to check the security tab on the folder that immediately showed the problem: IUSR_servername had no permissions checked and was therefore read-only. I set write permission & "hey-presto" WebClient.OpenWrite() worked !

Thank you.

One question though...The folder is a virtual web set up using IIS, so why doesn't IIS set the folder properties according to the security settings within IIS

Regards

Peter Darvas peterd@tibor.com





Re: .NET Framework Networking and Communication WebClient.OpenWrite()

Jorg Jooss - MSFT

Peter ¡ª

such a feature cannot work in all cases. If you use Windows Authentication with impersonation, there's no way of infering the desired NTFS permissions from the IIS process identity, because that identity is not being used when performing the actual I/O operation.




Re: .NET Framework Networking and Communication WebClient.OpenWrite()

peterdarvas

Hello Jorg

I have a related question you might be able to help with....

Prior to WebClient.OpenWrite() I need to choose the local client-side file to PUT onto the server. I have a C# aspx page with <asp:CheckBoxList id=XX> which I populate by

XX.Items.Add(new ListItem( fi[ i ].ToString()));

having got fi from:

DirectoryInfo di = new DirectoryInfo(folder);

FileInfo[] fi = di.GetFiles();

The question is how do I figure out the client-side "folder". If I just hard-code say "C:\\something" then that is assumed to be C: on the server. If I use

String folder = Environment.CurrentDirectory; then at least that is on the client, but is the directory from which the browser was launched (eg C:\Windows\system32).

My application is trying to work with files in a known & pre-existing directory say, "O:\\epc" so how do I get the DirectoryInfo to access it

Regards

Peter Darvas





Re: .NET Framework Networking and Communication WebClient.OpenWrite()

Jorg Jooss - MSFT

You cannot access a client's local file system from a browser unless you use an ActiveX control or restrict yourself to using HTML input controls.

And none of the APIs you mentioned will operate on a local file system path as long as the code runs on the server¡­