当代码进入第2行时,Windows将显示一个弹出窗口,输入用户名,密码进行TFS访问:
TeamProjectCollection = new TfsTeamProjectCollection(new Uri(url), new UICredentialsProvider());
TeamProjectCollection.Connect(Microsoft.TeamFoundation.Framework.Common.ConnectOptions.IncludeServices);
TeamProjectCollection.EnsureAuthenticated();
IsConnected = true;
有任何方法可以自动分配用户名,密码而无需从Windows弹出窗口登录。
感谢。
答案 0 :(得分:0)
如果您想直接插入用户凭据而不使用进程正在运行的凭据,以下内容对我有用:
WindowsCredential credentials = new WindowsCredential(new NetworkCredential(username, domain, password), new MyCredentials(username, domain, password));
TfsTeamProjectCollection connectedTPC = new TfsTeamProjectCollection(tfsUrl, new TfsClientCredentials(credentials));
提供两次凭证有点奇怪,但如果没有MyCredentials,我将无法从TFS得到答案,不确定原因。
public class MyCredentials : ICredentialsProvider
{
private NetworkCredential credentials;
#region ICredentialsProvider Members
public MyCredentials(string user, string domain, string password)
{
credentials = new NetworkCredential(user, password, domain);
}
public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
{
return credentials;
}
public void NotifyCredentialsAuthenticated(Uri uri)
{
// who cares
}
#endregion
}