如何删除 Windows 凭据

How to remove Windows credentials

使用以下命令,将显示所有存储的 Windows 凭据。

rundll32.exe keymgr.dll,KRShowKeyMgr

我正在寻找一种方法来删除单个凭据,例如特殊服务器的凭据。

C# Windows 应用程序应删除凭据。 我正在查看命名空间 System.Web.SecuritySystem.ServiceModel.Security,但这似乎不合适。

我想应该有一个托管的 Windows API 可以完成这项工作。

我该怎么做?

试试这个 NuGet 包:https://www.nuget.org/packages/CredentialManagement/。它可以获取、设置和删除 Windows 个凭据。

实际上,没有任何官方 C# API。这个 NuGet 包只是包装了必要的 DLL 文件调用。

Windows 图书馆的这一部分目前 尚未对全世界开放 。背后的原因很简单:我们正在使用 凭据,一旦我们将它们加载到 .NET 环境中,它们将立即由托管代码处理并易于访问(窃取)。

在此基础上,Microsoft 准备了程序 cmdkey(如 itsme86 在评论中 mentioned)。

The cmdkey.exe is a Credential Manager Command Line Utility.

This file is part of Microsoft® Windows® Operating System. Cmdkey.exe is developed by Microsoft Corporation. It’s a system and hidden file. Cmdkey.exe is usually located in the %SYSTEM% sub-folder and its usual size is 13,824 bytes.


如果你想在 C# 中使用它,需要创建自己的 API 周围(或者使用现有的,如果有的话)。对于目标列表,您可以执行以下操作:

const string cSplitString = "target="; // word "target" might differ in other languages

var targets = new List<string>();
var proc = new Process // We need separate process to get the output
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "cmdkey.exe",
        Arguments = "/list",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};
proc.Start();

// reading output from the process
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    if (line.Contains(cSplitString))
        targets.Add(line.Substring(line.IndexOf(cSplitString)+cSplitString.Length));
}

并删除:

string deleteTarget = "test";
Process.Start("cmdkey.exe", "/delete:" + targets.First(a => a.Contains(deleteTarget)));

我用过这个,它真的很管用。

Install-Package CredentialManagement -Version 1.0.2
    or
    dotnet add package CredentialManagement --version 1.0.2

会有用的。您可以从 this link.

下载