开发者

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

开发者 https://www.devze.com 2023-01-23 23:14 出处:网络
I am using this Impersonator class to impersonate a domain account to access a network share like so:

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.Pa开发者_如何学编程ssword = 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?


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);
}


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号