问题
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 :)
回答1:
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.
回答2:
I guess my comment is the answer I would offer:
technet.microsoft.com/en-us/sysinternals/bb897553.aspx
Please bear in mind I have not run your code on my machine but I am guessing you can see that you can run PSExec as a test and 'figure out' how to use it for your purpose.
I hope this is useful.
来源:https://stackoverflow.com/questions/11328410/write-files-to-a-location-from-another-user-and-domain