0

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!

Gerald
  • 1,033
  • 5
  • 20
  • 41
  • You should basically be able to substitute `new TeamFoundationServer` with `new TfsTeamProjectCollection`. That said: this code can't compile, since you reassign `credentials`. Two questions: what's a `MyCredentials`? What error message(s) do you get? – Edward Thomson Feb 10 '14 at 05:45
  • I edited my question. If I substitute, the `TfsTeamProjectCollection` will not accept the `credentials` parameter. – Gerald Feb 10 '14 at 05:58
  • I don't see the value of building an `ICredentialsProvider` just to return an `ICredentials`. Why not just use the constructor to `TfsTeamProjectCollection` that takes an `ICredentials`? – Edward Thomson Feb 10 '14 at 06:04

3 Answers3

1

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);
Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49
  • The first object that you create - the `NetworkCredential` - why don't you just pass that to `TfsTeamProjectCollection`? It takes an `ICredential`... – Edward Thomson Oct 05 '15 at 11:10
1

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();
Adam S
  • 73
  • 11
  • 2
    Is this an answer to the problem? While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit](http://stackoverflow.com/posts/44415139/edit) your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. [answer]. – Dev-iL Jun 07 '17 at 14:56
0

A detailed walkthrough is here.

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41