我试图从操作系统中删除旧证书,所以我为此写了一个方法:
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;
}
}
在执行过程中很不幸,我收到错误消息:
Message: Administrator permissions are needed to use the selected options. Use an administrator command prompt to complete these tasks.
CertUtil:请求的操作需要提升。
在Windows 7上,此代码有效。在Windows Server 2012上,如果我使用rmb然后以admin身份运行,那么如果我双击然后就不运行就可以了
我正在运行的用户在本地管理员组中