Search code examples
c#.netauthenticationconsole-applicationshared-directory

write files to a location from another user and domain


I m working on a console application that tries to download some files from a website by logging in and then copying the files to a shared folder.

Here the domain I am working in is different that the domain of that shared folder where I need to save the files.

Currently I overcome this problem manually by opening the shared folder from the run and putting the username and password into the windows authentication dialog and then running the application.

Here is the code I m using -

 static public void Start()
        {
            try
            {
                serverpath = ConfigurationSettings.AppSettings["serverpath"];
                if (flag == 0)
                {
                    username = ConfigurationSettings.AppSettings["UserNameFN"];
                    password = ConfigurationSettings.AppSettings["PassWordFN"];
                }
                else
                {
                    username = ConfigurationSettings.AppSettings["UserNameMFS"];
                    password = ConfigurationSettings.AppSettings["PassWordMFS"];
                    check = true;
                }

                string webUrl = "https://www.website.org/";
                string formParams = string.Format("user={0}&password={1}", username, password);
                WebRequest req = WebRequest.Create(webUrl);
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                req.Proxy.Credentials = CredentialCache.DefaultCredentials;
                byte[] bytes = Encoding.ASCII.GetBytes(formParams);
                req.ContentLength = bytes.Length;
                using (Stream os = req.GetRequestStream())
                {
                    os.Write(bytes, 0, bytes.Length);
                }
                WebResponse resp = req.GetResponse();
                cookieHeader = resp.Headers["Set-cookie"];

             (ConfigurationSettings.AppSettings["UserNamePROD"], ConfigurationSettings.AppSettings["PassWordPROD"], ConfigurationSettings.AppSettings["DomainPROD"]);
                Directory.CreateDirectory(serverpath + @"Log\");
                Logging.Write_Line(DateTime.Now + " - File Transfer Started");
                if ((!check) || (count == 0))
                {
                  string[] filename = getFileNames.returnFileNames(cookieHeader)
                }
             }

With above code

Directory.CreateDirectory(serverpath + @"Log\"); this line throws an exception.

How can I programmatically login to the shared location while running my application from my machine?

Thanks in advance :)


Solution

  • You might get away with it by executing the directory creation command as a shell process, like this (full namespaces provided in order to avoid cluttering up code with using directives):

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = 
        new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; // Hides the command prompt window
    startInfo.FileName = "cmd.exe";
    startInfo.Arguments = "/C mkdir " + serverpath + @"\Log";
    startInfo.Username = "domain\username";
    startInfo.Password = // SecureString object containing the password
    process.StartInfo = startInfo;
    process.Start();
    

    Not tested but logic suggests it might work.