【问题标题】:clear cached TFS client credentials清除缓存的 TFS 客户端凭据
【发布时间】:2019-05-29 10:13:59
【问题描述】:

请帮助我使用 C# 代码清除 TFS 缓存的凭据。我正在使用 TFS API 访问由位于 https://dev.azure.com 的 Dev ops TFS 服务器托管的源代码

有时(更改域密码后)访问源代码服务器时会引发 401 错误。请在下面找到用于连接的代码示例:

    var u = "https://dev.azure.com/orgid";

    var vssCred = new VssClientCredentials();
    if (cacheCred)
        vssCred.Storage = new VssClientCredentialStorage(); // tried with storage and without

    Logger.Debug("getting vsts collection for url:{0}", u);
    TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(u, vssCred);
    try
    {
        Logger.Debug("authenticating");

        tpc.Authenticate();
        tpc.GetService<VersionControlServer>();

它使用Microsoft.TeamFoundationServer.ExtendedClient.15.131.1Microsoft.TeamFoundationServer.Client.15.131.1 包。

我尝试使用如下代码清除缓存的凭据:

IEnumerable<string> ClearCachedTokens(VssCredentials cred, Uri address)
{
    if (cred == null) return null;
    var res = new Collection<string>();
    foreach (VssCredentialsType enumValue in Enum.GetValues(typeof(VssCredentialsType)))
        try
        {
            var token = cred.Storage.RetrieveToken(address, enumValue);
            if (token != null)
            {
                var tokenData = string.Join(";", token.Properties.Select(_ => string.Format("{0}={1}", _.Key, _.Value)));
                Logger.Debug("got token {0} {1}", enumValue, tokenData);
                cred.Storage.RemoveToken(address, token);
                res.Add(address.ToString());
            }
        }
        catch (Exception ec)
        {
            Logger.Warn("can't clear token type:{0} error:{1}", enumValue, ec.Message);
        }
    return res;
}

但它没有返回任何条目并且错误仍然存​​在。

但是,当我删除 %appdata%Local\Microsoft\Team Foundation\7.0\Cache 内容并运行 tf.exe get 命令时,错误消失了。它要求我输入登录名和密码,然后在执行 tpc.Authenticate(); 时不再显示 401 错误。

如何使用TeamFoundationServer.ClientTeamFoundationServer.ExtendedClient API 清除Cache 文件夹中的缓存凭据?

【问题讨论】:

    标签: tfs azure-devops


    【解决方案1】:

    凭据可能已保存在 Generic Credentials 组的凭据管理器存储区 (Control Panel\All Control Panel Items\Credential Manager) 中。

    【讨论】:

    • 我在通用凭据中找不到任何类似于 tfs vstsazure 的内容。另外我想提醒一下,清除%appdata%Local\Microsoft\Team Foundation\7.0\Cache 文件夹会使tf.exe 要求输入登录名和密码。所以我知道它存放的地方
    猜你喜欢
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2020-09-07
    • 2018-04-21
    • 2011-07-12
    • 2023-03-13
    • 1970-01-01
    • 2019-06-10
    相关资源
    最近更新 更多