0

I am trying to remove old certificates from the os so i wrote a method for that:

    public ActionResult DeleteOldCertificates(Session session)
    {
        try
        {
            return (DeleteAutority(session) == ActionResult.Success 
                ? Delete2018(session) == ActionResult.Success 
                    ? ActionResult.Success : ActionResult.Failure 
                : ActionResult.Failure);
        }
        catch (Exception ex)
        {
            session.Log(ex.Message);
            return ActionResult.Failure;
        }
    }

    public ActionResult Delete2018(Session session)
    {
        try
        {
            var constants = new Constants(session);

            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = $"/C CERTUTIL.exe -delstore MY Loi2018";
            startInfo.Verb = "runas";
            process.StartInfo = startInfo;
            process.Start();

            process.WaitForExit();

            session.Log($"Delete2018 ExitCode: {process.ExitCode}");
            session.Log($"Delete2018 Message: {process.StandardOutput.ReadToEnd()}");

            return process.ExitCode != 0 ? ActionResult.Failure : ActionResult.Success;
        }
        catch (Exception ex)
        {
            session.Log(ex.Message);
            return ActionResult.Failure;
        }
    }
    public ActionResult DeleteAutority(Session session)
    {
        try
        {
            var constants = new Constants(session);

            Process process = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = $"/C CERTUTIL.exe -delstore -enterprise root Autority";
            startInfo.Verb = "runas";
            process.StartInfo = startInfo;
            process.Start();

            process.WaitForExit();

            session.Log($"DeleteAutority ExitCode: {process.ExitCode}");
            session.Log($"DeleteAutority Message: {process.StandardOutput.ReadToEnd()}");

            return process.ExitCode != 0 ? ActionResult.Failure : ActionResult.Success;
        }
        catch (Exception ex)
        {
            session.Log(ex.Message);
            return ActionResult.Failure;
        }
    }

Unfortunatelly during execution I am receiving error:

Message: Administrator permissions are needed to use the selected options.  Use an administrator command prompt to complete these tasks.

CertUtil: The requested operation requires elevation.

On the windows 7 this code works. On the Windows server 2012 if i use rmb and then run as admin then it is working if I just double click then not

user I am running this is in local admins group

szpic
  • 4,346
  • 15
  • 54
  • 85
  • @damagedCoda as I wrote in the question I need to check this on w7 and windows server 2012 and I only have problems with windows server 2012 – szpic Feb 28 '19 at 11:44
  • Damn my bad. So sorry. – damagedCoda Feb 28 '19 at 11:57
  • Do refer to [this](https://stackoverflow.com/questions/30080058/how-do-i-run-my-c-sharp-service-with-elevated-uac-privileges-on-windows-2012r2) post, it does not have a solution but i think it can help you understand your problem better. – damagedCoda Feb 28 '19 at 12:03

0 Answers0