11

Currently I have an application that receives an uploaded file from my web application. I now need to transfer that file to a file server which happens to be located on the same network (however this might not always be the case).

I was attempting to use the webclient class in C# .NET.

    string filePath = "C:\\test\\564.flv";
    try
    {
        WebClient client = new WebClient();

        NetworkCredential nc = new NetworkCredential(uName, password);

        Uri addy = new Uri("\\\\192.168.1.28\\Files\\test.flv");
        client.Credentials = nc;
        byte[] arrReturn = client.UploadFile(addy, filePath);
        Console.WriteLine(arrReturn.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

The machine located at 192.168.1.28 is a file server and has a share c:\Files. As of right now I am receiving an error of Login failed bad user name or password, but I can open explorer and type in that path login successfully. I can also login using remote desktop, so I know the user account works.

Any ideas on this error? Is it possible to transfer a file directly like that? With the webclient class or maybe some other class?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
JustLogic
  • 1,738
  • 2
  • 12
  • 24
  • is the account a domain account, or a machine account? – TheSoftwareJedi Nov 04 '08 at 21:40
  • The account is a user on the machine. The issue is we are moving to a new architecture and we have 2 file servers clustered and load balanced with a virtual ip. So I need to be able to send the file to the VIP. – JustLogic Nov 04 '08 at 21:53
  • 2
    I know using web client works if I post to a page. But I want to avoid that and directly transfer the file. Is there a way to do that? – JustLogic Nov 04 '08 at 21:55

3 Answers3

17

Just use

File.Copy(filepath, "\\\\192.168.1.28\\Files");

A windows fileshare exposed via a UNC path is treated as part of the file system, and has nothing to do with the web.

The credentials used will be that of the ASP.NET worker process, or any impersonation you've enabled. If you can tweak those to get it right, this can be done.

You may run into problems because you are using the IP address instead of the server name (windows trust settings prevent leaving the domain - by using IP you are hiding any domain details). If at all possible, use the server name!

If this is not on the same windows domain, and you are trying to use a different domain account, you will need to specify the username as "[domain_or_machine]\[username]"

If you need to specify explicit credentials, you'll need to look into coding an impersonation solution.

TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
  • Maybe it won't work because of the permissions. He can use System.IO.File.Copy only if he run the website under a user which has the needed permissions. By default the web application is running under a local, limited user in order to avoid security issues. – Biri Nov 04 '08 at 21:37
  • Added some permission details. Agreed - permissions can get tricky. – TheSoftwareJedi Nov 04 '08 at 21:40
  • @TheSoftwareJedi Is it possible I can transfer the file to file server without storing on the server where file initially uploaded from web page? – user786 Feb 05 '17 at 14:50
4
namespace FileUpload
{
public partial class Form1 : Form
{
    string fileName = "";
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        string path = "";
        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Title = "Attach customer proposal document";
        fDialog.Filter = "Doc Files|*.doc|Docx File|*.docx|PDF doc|*.pdf";
        fDialog.InitialDirectory = @"C:\";
        if (fDialog.ShowDialog() == DialogResult.OK)
        {
            fileName = System.IO.Path.GetFileName(fDialog.FileName);
            path = Path.GetDirectoryName(fDialog.FileName);
            textBox1.Text = path + "\\" + fileName;

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            WebClient client = new WebClient();

            NetworkCredential nc = new NetworkCredential("erandika1986", "123");

            Uri addy = new Uri(@"\\192.168.2.4\UploadDocs\"+fileName);

            client.Credentials = nc;
            byte[] arrReturn = client.UploadFile(addy, textBox1.Text);
            MessageBox.Show(arrReturn.ToString());

        }
        catch (Exception ex1)
        {
            MessageBox.Show(ex1.Message);
        }
    }
}
}
animuson
  • 53,861
  • 28
  • 137
  • 147
1

when you manually open the IP address (via the RUN command or mapping a network drive), your PC will send your credentials over the pipe and the file server will receive authorization from the DC.

When ASP.Net tries, then it is going to try to use the IIS worker user (unless impersonation is turned on which will list a few other issues). Traditionally, the IIS worker user does not have authorization to work across servers (or even in other folders on the web server).

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86