-1

I need to remove saved credentials of network shares from the windows cache programmatically.I searched for it but couldn't find a solution. But I solved this issue by executing the following statement in command prompt.

net use \\someloaction /del

Now i need to execute the cmd statement using asp.net c#.

I tried the following code. But its not working.

var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        var process = new Process { StartInfo = startInfo };

        process.Start();
        process.StandardInput.WriteLine("net use \somelocation /del");
        process.StandardInput.WriteLine("exit");

        process.WaitForExit();
Nachikethas
  • 43
  • 1
  • 8

1 Answers1

0

You should use the Arguments property of ProcessStartInfo class. The /C flag serves to pass the arguments to the Shell Command Processor for execution otherwise it will close immediately.

var startInfo = new ProcessStartInfo
{
    FileName = "cmd.exe",
    Arguments = @"/C net use \\somelocation /del",
    UseShellExecute = false,
    CreateNoWindow = true
};

However, the NET USE .... /D command doesn't remove the CREDENTIALS stored for your user to access the network share. If you need to remove the credentials, forcing the user to reinsert them the next time that he/she wants to use the shared resource you need to work with the Credential Manager.

The problem is that this library has no official managed implementation (AFAIK). So you need to use a plugin library that you can download from here or use the NuGet install with the command Install-Package CredentialManagement

So, after removing the sharing with the previous NET USE command, you could write in your code something like this

Credential cm = new Credential(@"\\server\share","", @"server");
cm.Delete();

You could find another question about this at this link
(Tried this library and I am able to clear the credentials to access a shared network on my lan using a command prompt. I hope the same happens on your side)

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • so after the process.Start(); statement what should i do? – Nachikethas Jan 29 '15 at 12:16
  • I started the proces and ended it. But still its not cleared. I also tried providing WorkingDirectory = "C:\\Documents and Settings\\myloginname". But no use. – Nachikethas Jan 29 '15 at 12:22
  • Re-reading your question it is not clear what you want to delete. The `Net use` command connects or disconnects the computer from a shared resource. It has nothing to do with deleting CREDENTIALS – Steve Jan 29 '15 at 12:54
  • Forget to add the @ at the beginning of the parameters line – Steve Jan 29 '15 at 13:05
  • My objective is to access files on remote server(which uses $share) using admin credentials in c#.. But once i access the file ,the credentials seems to be cached in windows. The very next time I access the same file with wrong credentials I am able to get the files because the right credentials are already in the windows. So i need to remove these credentials. – Nachikethas Jan 29 '15 at 13:11
  • According to this article http://www.windowsnetworking.com/kbase/WindowsTips/WindowsServer2008/AdminTips/Network/DeleteLoginCredentialsforaNetworkShare.html you should be able to do it with your NET USE command. Please check again after the fix for the verbatim character @ – Steve Jan 29 '15 at 13:18
  • Actually i referred this site. I am able to delete it using cmd prompt. But using c# code is where i find it difficult. Also given '@' in front.But still not working. – Nachikethas Jan 29 '15 at 13:23