Link to home
Start Free TrialLog in
Avatar of pavelmed
pavelmed

asked on

How to upload a file to a Samba share using ASP.NET application on Windows 2003 Server?

I have to upload a file to a Samba share using the ASP.NET application on Windows 2003 Server.

If I needed just to upload a file to save it on the server's folder, I would just use either the PostedFile.SaveAs("finename") or
HttpFileCollection MyFileCollection = Request.Files  and then MyFileCollection[index].SaveAs("FileName")
- both ways worked OK for the server's folder.  But I can't provide a samba share description in place of the file name because the user name and password are needed for the share, and the format of these method does not allow it.

How should I do it?  My idea was to run a batch file to map a samba share and then upload the file to that map.
It worked OK on my Windows XP server, but it does not work on the Windows 2003 Server. (the batch file contained just one statement: "Net use Y: \\bosfhristest51\hrimports <password> /USER:<domainname>\<username>"

However the Windows 2003 Server throws "Access is denied" exception when I try to execute ANY batch file, even the simpliest one. (I execute the batch file using the following code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName="E:\\IntranetSites\\UploadHR\\CreateMap.bat";
proc.Start();

Again, this works on a Windows XP server but does not work on a Windows 2003 Server.

I am asking EITHER for another way to upload a file to a samba share OR for detailed and specific instructions how I should set the Windows 2003 Server to execute a batch file.  So far my attempts to do it were unsuccessful.

How can I do it ???

Thank you in advance for your help !

Avatar of mmarinov
mmarinov

Hi pavelmed,

is the user that executes this has rights for execute/modify in this category ?

Regards!
B..M
mmarinov
Avatar of pavelmed

ASKER

The users who will access the application and upload the file, will have all necessary permissions to do it.
But right now (so far) the application does not provide an authentication for a user who accesses it.
I'll have to set all necessary rights and permissions for the right user, but first I need to find out what rights and permissions are needed to do the job, and how and where to set them.

Right now I am just trying to execute any batch file that does not do anything special, even like that: "echo bla-bla-bla", and still the access is denied on the Win 2003 server.  And if the special permissions are needed for that, could you please describe what I should do?

I played with the impersonation on both my Windows XP server and network Windows 2003 server, but it did not work anywhere.
On my local server I tried to use the impersonation setting with and without credentals, and to execute a simple batch file to copy a file from one local directory to another one.
Without the impersonation the file was copied.
With the impersonation the file was not copied.  I tried both:
<identity impersonate = "true" />     or
<identity impersonate = "true" userName="myname" password="mypass" /> where myname and mypass belong to the administation group, however, the file was not copied in both cases.

So the impersonation did not work for me even on a local server.  I might have done something wrong with the impersonation setting, but I don't see what.

And I don't know any other way to deal with samba share but to map a share and save a file to the map.

I would appreciate any your suggestions.

Thank you.
pavelmed,

try to give modify writes on the IUSR_... user in the current directory

Regards!
B..M
mmarinov
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mmarinov,

Thank you, I finally got the simple batch file working!  But still can't create a map...
I can now run the file that simply copies a file from one local directory to another one (on a Windows 2003 server).
To accopmlish that, I used the Brandon's code from the site
http://dotnetjunkies.com/WebLog/bsblog/archive/2004/05/13/13484.aspx 
and a suggestion to set
<identity impersonate = "true" /> from another user of the same site.

Actually, I had already visited that site before and even tried the Brandon's code, but without success.
A had also unsuccessfully tried <identity impersonate = "true" /> before.

The difference was that I tried them separately.  The code did not work without identity impersonate = "true" setting, and the setting did not work without Brandon's code.
In order to run that simple batch file (just to copy a file), it was not necessary to set "modify" rights on the IUSR_... user in the current directory.  I did it first, then removed, and it still worked.

So here again is the main problem: to create a map.  The code runs, no exceptions are thrown, but there is no map.

I tried to redirect the StandardError similarly to redirecting StandardOutput in Brandon's code and then display the error.

But the program hangs if the following statement is included.
string ErrResults = sErr.ReadToEnd().Trim();

The batch file to create a map contains just one statement:
Net use Y: \\<server_name>\<folder_name> <password> /USER:<domain>\<username>

It this batch file runs OK from the ASP.NET application on my local Windows XP server.
It also runs OK if I run it directly on the Windows 2003 server (from the command prompt or Start/Run box).

Here the security issues might be involved, but I am not sure how to deal with them.

Could you please help???

Thanks
to check if this is a securty issue:
if your IIS is less then 6 then go to the machine.config file in your
%system root%\microsoft.net\framework\v.1.1.xxxx\config\machine.config
search for the processModel and set username to SYSTEM ( it is case sensitive )
if you use IIS 6 then in your application ppol set to be executed under Local System

these are accounts that have full permissions ( it is nor recomended to use them in normal scenarios )

B..M
mmarinov
When you say "your IIS", do you mean the IIS on that Windows 2003 server?  If yes, IIS manager there is version 6.0
On my Windows XP (development server) the IIS is v.5.1
The application should run on the Windows 2003 server, so I assume you are referring to it.

Could you please specify " in your application ppol set to be executed under Local System" ?

Also, I have to restrict a number of users to run this application to just a few names.

Thank you


open your IIS console ( on the machine where you try to execute the bat )
right click on your virtual directory/web site ->Properties - in the Directory ( or Home Directory ) tab on the bottom there is a dropdownlist with selected application pool for this virtual directory/web site
remember this application pool name and close this window
in the tree in the IIS Console above the web sites there is an Application pool node
expand it and find the remembered application pool's name
right click on it and select Properties
go to Identity tab and choose from the dropdown list Local System

B..M
mmarinov
B..M,

The only thing I have found on the virtual directory/web site ->Properties -Virtual Directory tab was an "Application Protection" drop-down box with "Medium (pooled)" selected.  Is it the application pool name?  I checked on my local Windows XP server where the same application is also installed (and runs OK), and it the same.
There is no Application pool node above the web sites on both IIS Consoles (on both machines).

Do we have different IIS versions?

Thank you

i think you don't use the IIS 6.0, but
when you open the IIS Console you have to se

Internet Information Services
  name of the webserver
     Application pools
     Web sites
     ....

so if there is no Application pools
you can try the other scenario

B..M
B..M,

From the Internet Information Services (IIS) Manager I click on Help/About IIS Manager, and  the version is 6.0.
The tree is like the following:

Internet Information Services
  name of the webserver
      Web sites
        ....
        ....
      Web Service Extensions

There is no Application pools node...

What other scenario do you suggest?  For the machine.config file changes?

Thank you
yes - machine.config
just set the UserName="SYSTEM" ( remember it is case sensitive ) to check if it will work - and return it back to the original - because the SYSTEM has full rights on the computer
if it work - then it is something with the rights
if not - i can not think of anything else

B..M
mmarinov
I changed the UserName to "SYSTEM" for this test.  However, I still got the exception "The specified network password is not correct." while executing the CreateMap.bat file.  (The same exception is thrown when I have the default setting of the UserName to "machine".)

If I run the file on the server from the command prompt or Start/Run box, it creates a map.
What else can cause the problem?

Thank you.
the error talks: the user with this password is not correct - it is not with the execution of the asp.net but with security from your machine to the server

B..M
mmarinov
But this batch file uses the right password and creates a map if I run it from the command prompt or Start/Run box on the same server.  Or if I just run the statement from the batch file on from the command prompt.
B..M,
The update:
There was something wrong with a server for a while, and it had a disconnected map drive that just spoiled the picture for about an hour, and that I could not delete because it did not map to anything.  I was able to delete it only after I rebooted the server.

Now it behaives again as foolows:
The map is created if I run the batch file from the command prompt or Start/Run box on the same server.
But the map is not created if I try to run it from the ASP.NET application using both "machine" and "SYSTEM" userNames in the machine.config file.  And there are no exception thrown.  The aplication waits for a map to be created for 5 seconds, then checks for the existence of the mapped drive.  And it is not there.  The application hangs when I try to send the Output or Errors (both are redirected) to a string to be displayed using the statements
System.IO.StreamReader sErr = proc.StandardError;
string ErrResultsErr =  sErr.ReadToEnd();   //hangs if this statement is included in the code

or

System.IO.StreamReader sOut = proc.StandardOutput;
string ErrResultsOut = sOut.ReadToEnd();    //hangs if this statement is included in the code

Thank you
in the first posted link there was :
Process.Start("cmd.exe", "/C C:\\Inetpub\\myCmd.bat");
also as i've read - why ton't you use
proc.StartInfo.UseShellExecute = true;
?

b..m
mmarinov
Answering your questions:
1) I could not compile
Process proc = new Process();
proc.Start("cmd.exe", "myFile.bat");     - because Start(string) is a static method.

I could compile
Process proc = new Process();
proc.StartInfo.FileName="myFile.bat";
proc.Start ( );   - but it did not work - Access Denied

System.Diagnostics.Process.Start("cmd.exe", ="myFile.bat");    - did not work either - Access Denied

2) I tried proc.StartInfo.UseShellExecute = true;
Same result - Access Denied.

Anyway, I did create a map!!!  I used Brandon's way of coding (see http://dotnetjunkies.com/WebLog/bsblog/archive/2004/05/13/13484.aspx ), the same that I used just to copy a file.

I thought It did not work to create a map, but in fact I was wrong - it worked.  I was confused because the application did not recognize and find this map.  So it could not use it.  And so far the only solution to use this map is to continue using other batch files or DOS statements passed to cmd window with WriteLine( ) method.
But I do need to check for the existence of this map in the app...

It's interesting to note that on the Windows XP server, where I could use the more common code like
Process proc = new Process();
proc.StartInfo.FileName="myFile.bat";
proc.Start ( );  
........................
The appliction recognized the map created that way.  But I am trying to find the way to deal with writig directly to the cmd window to get the map recognized.  Do you have any ideas?
I appreciate your help a lot, and I will accept one of your previous answers even if you can't help me any more.
But so far I did not finish working on this issue.

Thank you
pavelmed , i'm glad that you have found some solution, but:
1. i think that when you create the map it can be used only through the process that is created under - just an opinion
2. i'm stuck with the recognision and unfrotunately i could not help any more, sorry :(
3. as for the points - i'm not here for the points but for the help, and the last words in your previous post "Thank you" is bigger award than point :) that is EE stands for

again sorry that could not help any further

Regards!
B..M
mmarinov