'Upload files to a folder inside a domain, by a user outside de domain

This part of my code works if the application are working in the same domain as targetpath. But I need the application to work outside the domain, and to be possible to copy files to a folder inside the domain.

When I try to upload a file does not work because the folder access needs user and password to access the folder. If I try to open the targetpath outside the domain, Windows asks to enter credentials.

So, how can i pass the user and password on code?

    private void btnAnexar_Click(object sender, EventArgs e)
    {
        String input = string.Empty;
        OpenFileDialog dialog = new OpenFileDialog();

        dialog.Filter ="All files (*.*)|*.*";

        dialog.InitialDirectory = "C:";
        dialog.Title = "Select a text file";

        if (dialog.ShowDialog() == DialogResult.OK)
            input = dialog.FileName;

        try
        {
            string fileName = dialog.SafeFileName;
            string extension = Path.GetExtension(fileName);
            string sourcePath = Path.GetDirectoryName(input);
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);

            copia = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + extension;

            MakeUnique(targetPath + "\\"+ copia);

            string destFile = System.IO.Path.Combine(targetPath, copia);


            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            System.IO.File.Copy(sourceFile, destFile, true);
            linkLabel1.Text = "...\\" + copia;

            if (input == String.Empty)
                return; //user didn't select a file to open
        }
        catch (Exception e3)
        {
            Console.WriteLine(e3.ToString());
        }
    }


Solution 1:[1]

The user you are running the program under needs to have Write permissions to the target folder. You need to either be a user with permission to change read/write permissions on the target directory or have the credentials for such a user to do this in code.

Solution 2:[2]

You can use UploadFiles.On.External.Domain nugget package :

  1. ConfigureServices :
services.AddSingleton<IUpLoadFile, UpLoadFile>();
  1. Write this code in appsettings.json :
"UploadFileSettings": {
    "Domain": "yourdomain.com",  
    "Root": "root//www//files, 
    "Separator":"//", 
    "MaximumSize": 5 
},
"Domain": "your domain name",
"Root": "your server root", 
"Separator":"{for Windows //}, {for Linux \\}",
"MaximumSize":  "number by MB.",

  1. Inject IUpLoad in constructor:
private readonly IUpLoadFile UpLoadFile; 
public testController(IUpLoadFile iUpLoadFile)  { UpLoadFile = upLoadFile; } 
  1. Use functions
     I- UpLoadFile.UpLoad(); 
    II- UpLoadFile.UpLoad2(); 
   III- UpLoadFile.Delete();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dmck
Solution 2 Elikill58