TFS api未验证凭据

时间:2014-02-10 02:44:05

标签: c# tfs tfs-sdk

我在Windows窗体应用程序中有以下代码:

        // Connect to server
        var tfs = new TeamFoundationServer(tfsServer, credentials);
        try
        {
            tfs.EnsureAuthenticated();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        };

这很有效。问题是TeamFoundationServer是一个过时的类。

The TeamFoundationServer class is obsolete. Use the TfsTeamProjectCollection or TfsConfigurationServer classes to talk to a 2010 Team Foundation Server. In order to talk to a 2005 or 2008 Team Foundation Server use the TfsTeamProjectCollection class.

我现在使用以下代码,但它不验证我的凭据。我在这里做错了什么?

        NetworkCredential credentials = new NetworkCredential(username, password, domain);

        MyCredentials credentials = new MyCredentials(username, password, domain);
        TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer), credentials);
        try
        {
            tfs.EnsureAuthenticated();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

这是MyCrendentials类:

    private 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)
        {
            throw new NotImplementedException();
        }

        #endregion
     }

提前致谢!

3 个答案:

答案 0 :(得分:1)

今天早上我在同样的事情上挣扎。您不再需要任何接口/实现代码。这就是我开始工作的方式:

// Translate username and password to TFS Credentials
ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
WindowsCredential windowsCredential = new WindowsCredential(networkCredential);
TfsClientCredentials tfsCredential = new TfsClientCredentials(windowsCredential, false);

// Connect to TFS Work Item Store
Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, tfsCredential);
WorkItemStore witStore = new WorkItemStore(tfs);

答案 1 :(得分:1)

我知道这是一个老问题,但我一度都在问同样的问题。

NetworkCredential credentials = NetworkCredential("User", "Password", "Domain");
TfsConfigurationServer tfs = new TfsConfigurationServer(new Uri("Address"), credentials);
tfs.Authenticate();

答案 2 :(得分:0)

详细的演练是here