2

My web applications are on Server D. So I created a web application that has an fileupload control. I can easily upload any file to a folder (FILES) inside the web app.

My question now: How can I upload the same file (from code behind) to a folder (UPLOADS) on a different server (SERVER T) that I have? I also need to read,write and modify that file on the SERVER T. Remember that I use Web Forms and not Windows Forms!!!

Someone told me something about creating a Network Service account on server T for the web app on server D to use and give that account permissions or something like that but he doesn’t know the details either.

Is there a tutorial, guide since I'm sure there is a way to do this…

Also, all I can find on Google is using ftp but for windows forms only…

Kinda stuck around here…

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Nathan
  • 24,586
  • 4
  • 27
  • 36
  • Does the target server has shared folder which can be accessed by original server? – Yahya Mar 20 '13 at 16:12
  • @DavidStratton That assumes the servers 'know' about each other, and I would always say this article demonstrates the way _not_ to do it; create an intermediary vector for communication, such as a service. – Grant Thomas Mar 20 '13 at 16:13
  • @Yahya, yes to you question. The 2 servers are in the same network and they can access eachother //ServerT //ServerD – Nathan Mar 20 '13 at 16:18
  • @Grant Thomas, the servers do "know" each other... how would i create a service like the one you suggested. do you have a link ? – Nathan Mar 20 '13 at 16:19
  • 1
    @Nathan Try this then: http://growingtech.blogspot.co.uk/2012/06/copy-network-shared-folder-file-using-c.html – Yahya Mar 20 '13 at 16:20
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Mar 20 '13 at 16:33
  • 1
    @GrantThomas, While creating service to deal with manipulating files is useful suggestion at the end last server in the chain of services will have the same exact problem - file storage on one server, service talking to that is on another (if you do proper isolation). So I think [article](http://2leggedspider.wordpress.com/2007/05/28/upload-files-to-unc-share-using-asp-net/) that David Stratton linked to would give good starting point for that last segment. – Alexei Levenkov Mar 20 '13 at 16:33
  • @yahya thanks man that was it: the blog you gave me with the impersonation did the trick... – Nathan Mar 21 '13 at 14:22
  • @AlexeiLevenkov thanks Alex...your link had a second solution similar to yahya's article and it totally worked. thanks again... – Nathan Mar 21 '13 at 14:24
  • @Nathan my pleasure, I will post it as an answer so it can help others in future! – Yahya Mar 21 '13 at 14:29

3 Answers3

2

If you need to read/write/change files in a network folder, you can try this:

Open IIS > Application Pools 
            > Select Application pool of the site
              > Advanced Options
                > Process Model > Select Identity

Now you should know the Application Pool Account. Finally make sure that account does have the required privileges to SERVER T\UPLOADS folder.

Kaf
  • 33,101
  • 7
  • 58
  • 78
  • Note: It is not going to work if site is using impersonation. – Alexei Levenkov Mar 20 '13 at 16:27
  • ? I'm sorry if my comment looked like "your answer is wrong" - all I wanted to is remind future readers that this approach may not immediately work for sites. Impersonation/"NTLM one-hop-hell" is very hard already, so being aware when some solution don't apply could be useful. – Alexei Levenkov Mar 20 '13 at 16:38
  • @AlexeiLevenkov, ah,.. that is okay. Extra information always welcome ! – Kaf Mar 20 '13 at 16:39
  • 2
    +1 - actually this is vary safe approach to have credentials to talk to other Windows servers (assuming one need just one set of credentials - i.e. talking to single DB/file share). As I've noted - if site uses impersonation more work would be needed to revert impersoantion to run as process' account. – Alexei Levenkov Mar 20 '13 at 16:41
  • @Kaf In the select Identity drop down there were only 3 options: Network Service, Local Service and Local System. I chose Network Service and then i made sure that the folder has NETWORK SERVICE group/user in the security tab with full permissions. But i still get the same error in my err.Message: Access to the path '\\ServerT\Uploads\file.dbf is denied'. WHen i run this on my localhost, everything works fine though... – Nathan Mar 20 '13 at 18:20
  • You can access `Uploads` folder like that only if you have shared it. Otherwise you have to access like `\\ServerT\c$\Uploads\..` where `c` is the local drive. I am not a security expert but I think you may need to share it and give full control to the network services. If not working try to use a custom user. – Kaf Mar 21 '13 at 09:07
  • @Kaf Uploads is shared...as i said Everything works fine when i compile my project locally...the problem shows up when i go live with it. Also, when i copy //serverT/Uploads/Files/file.dbf in the URL it automatically downloads it. WHen i copy //serverT/Uploads/Files/ i can see all the files listed as well... – Nathan Mar 21 '13 at 12:33
  • That means `virtual path` is working and you have allowed `directory browsing`. Can you try this using a custom account: Create a new account (or even existing one) and make a member of `IIS_IUSRS` user group in IIS running machine. Then select that user for the `Application Pool`. Next give him permission to above shared folder. – Kaf Mar 21 '13 at 13:02
2

Taken from this blog post

We can do following-

  1. Login to a user account using c#
  2. Impersonate as the logged in user
  3. Copy the file using System.IO.File.Copy
Yahya
  • 3,386
  • 3
  • 22
  • 40
0

A very simple solution would be to simply ftp the file to the new server, here is an example

http://www.codeproject.com/Articles/343913/Simple-FTP-library-in-Csharp

or doing an http post to the other server here is a SO question with a decent answer for doing that

FileUpload to FileStream

once the file is on the other server push the work of update/modify/etc to the other server

Community
  • 1
  • 1
Bob The Janitor
  • 20,292
  • 10
  • 49
  • 72