Accessing network share via Process.Start(path) using network credential

隐身守侯 提交于 2021-01-28 03:53:30

问题


I am using this Impersonator class to impersonate a domain account to access a network share like so:

using(new Impersonartor(username, domain, password))
{
//Code Here
}

Copying the file from the network share works okay:

using(new Impersonartor(username, domain, password))
{
 CopyAll(uncPath, localPath)
}

However, using Process.Start to view the UNC share in Explorer throws a "Logon failure: unknown user name or bad password":

using(new Impersonartor(username, domain, password))
{
 Process.Start(uncPath)
}

Suspecting that the Impersonator class is at fault, I tried manually supplying the credentials to ProcessStartInfo like so:

                        System.Diagnostics.ProcessStartInfo viewDir = new System.Diagnostics.ProcessStartInfo(uncPath);
                        viewDir.UseShellExecute = false;
                        viewDir.Domain = netCred.Domain;
                        viewDir.UserName = netCred.UserName;
                        viewDir.Password = ConvertToSecureString(netCred.Password);
                        System.Diagnostics.Process.Start(viewDir);

Still no joy. Note that I'm sure that my netCred (NetworkCredential) is correct as I've used to make prior connections to authenticated resources.

So, how do I view a UNC path in Explorer using a network credential?


回答1:


I had the same problem today and here is what worked for me:

private void OpenNetworkPath(string uncPath)
{
   System.Diagnostics.Process.Start("explorer.exe", uncPath);
}



回答2:


Instead of passing the uncPath to the Process.Start, try starting "explorer" in Process.Start and pass the uncPath as ProcessStartInfo's Arguments property.

System.Diagnostics.ProcessStartInfo viewDir = new System.Diagnostics.ProcessStartInfo("explorer.exe");
viewDir.UseShellExecute = false;
viewDir.Domain = netCred.Domain;
viewDir.UserName = netCred.UserName;
viewDir.Password = ConvertToSecureString(netCred.Password);
viewDir.Arguments = uncPath;
System.Diagnostics.Process.Start(viewDir);


来源:https://stackoverflow.com/questions/4132035/accessing-network-share-via-process-startpath-using-network-credential

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!