0

I have tried multiple different ways of transferring a file from a server to another different server which is on the same domain. No matter what I try I keep getting the incorrect username or bad password error, however when I try access the "\serverIP\c$" folder manually from the server I am able to access the folder correctly with the right username and password.

The first part of my code places the file from the local pc to the server where the application is hosted, and this works perfectly :

                string path = Path.Combine(Server.MapPath("~/ACAD_Drawings"),
                                           Path.GetFileName(file.FileName));
                file.SaveAs(path);

However I then need to move this file from this server onto a different server which will be using the file, and my last attempt was carried out using the following code:

                NetworkCredential myCred = new NetworkCredential("Username", "Password", "DomainName");
                WebClient webclient = new WebClient();
                webclient.Credentials = myCred;
                string tempFileForStorage = path;
                file.SaveAs(tempFileForStorage);
                webclient.UploadFile("\\\\NewServerIP\\c$", "PUT", tempFileForStorage);
                webclient.Dispose();
                System.IO.File.Delete(tempFileForStorage);

With this code I keep getting the incorrect username and password when I am sure that they are correct. Would anyone know if I am doing anything wrong or missing any steps?

Maeglin77
  • 173
  • 1
  • 14
  • 1
    Are you just copying a file? why would you use a webclient? – BugFinder Feb 01 '17 at 11:55
  • Have a look at http://stackoverflow.com/questions/3700871/connect-to-network-drive-with-user-name-and-password this will show you how to ADD creds to a resource, then its just standard .net file IO operations. – Steve Drake Feb 01 '17 at 11:56
  • @BugFinder I've tried many different ways, even using a simple File.Copy but since it is from one server to another it keeps throwing the username or password error each time – Maeglin77 Feb 01 '17 at 12:32
  • @SteveDrake I will check it out thanks – Maeglin77 Feb 01 '17 at 12:33
  • Did you map the folder with the credentials? – BugFinder Feb 01 '17 at 12:50
  • @BugFinder I am able to access one server folder via the other server as they are both on the same domain and using the same logon credentials. Or do you mean some way of mapping via C#? – Maeglin77 Feb 01 '17 at 13:17
  • I meant like map f:\ \\servername\share - if your app runs as anything other than your ID, then it wont use your id to do the mapping unless you supply the credentials – BugFinder Feb 01 '17 at 13:33
  • However I am supplying the credentials through the webclient, but I will try that out since at the moment I'm giving every option a shot. – Maeglin77 Feb 01 '17 at 14:06

1 Answers1

0

1st in your quest: you need know SMB/CIFS protocol. See : https://en.wikipedia.org/wiki/Server_Message_Block , how to connect(@Steve Drake) : Connect to network drive with user name and password

2nd when you connect on another computer use SMB, you can read/write remote files use normal IO like local computer.

3nd if two servers are not in a same LAN, use other way to transfer files will be better, like socket, WebAPI etc.

Community
  • 1
  • 1
Johan Shen
  • 158
  • 6