Gerald
Gerald

Reputation: 1083

TFS api not validating the credentials

I have the following code on my windows form application:

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

This is working. The problem is TeamFoundationServer is an obsolete class.

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.

I am using the below code now but it doesn't validate my credentials. What did I do wrong here?

        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);
        }

This is the MyCrendentials class:

    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
     }

Thanks in advance!

Upvotes: 0

Views: 3605

Answers (3)

Adam S
Adam S

Reputation: 73

I know it's an old question, but I was asking the same question at one point.

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

Upvotes: 1

Riegardt Steyn
Riegardt Steyn

Reputation: 5701

I struggled with the same thing this morning. You don't need any of that interface/implementation code anymore. This is how I got it to work:

// 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);

Upvotes: 1

Giulio Vian
Giulio Vian

Reputation: 8343

A detailed walkthrough is here.

Upvotes: 0

Related Questions