【发布时间】: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.1 和Microsoft.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.Client 或TeamFoundationServer.ExtendedClient API 清除Cache 文件夹中的缓存凭据?
【问题讨论】:
标签: tfs azure-devops