Connect to a TFS service without entering LiveID credentials

Original author: Buck Hodges
  • Transfer
Typically, when connecting to Team Foundation Service, a user sees a web page to log in with a Microsoft account, also called LiveID. By logging in, the user can select the checkbox to save their Microsoft account. In this case, it will not have to be re-entered unless long breaks are allowed between login sessions (then the system will request the data again).
This is very convenient for people, but what if the connection needs to be made for an application or web service? To this end, the program code should use "alternative credentials", the use of which must be allowed in the account settings. This is the same parameter that is used when enabling basic authentication for git-tf. Then you can write program code in which these credentials will be used to connect to the service. In the long run, we will also add OAuth support, however, it is still in the experimental stage.


Enable alternate credentials.



First of all, this function must be enabled. First, open your account or project in a browser, click your name in the upper right corner, and then click My Profile.



In the User Profile dialog box, click the Credentials tab.



Enter the password and save the changes.



Using alternate credentials in your code
Before performing the steps below, make sure that Visual Studio 2012 with Update 1 or later is installed on your computer. This update includes extensions to the TFS client object model that support the use of alternate credentials.
The easiest way to get the latest updates is to click the pop-up notification on the Windows taskbar or in Visual Studio go to Tools -> Extensions and Updates ..., click Updates, then Product Updates ( Product Updates) and install the latest update. This update can also be downloaded here .
To verify that you have installed Visual Studio with Update 1 or later, select Help -> About Microsoft Visual Studio.



Having finished setting up the credentials, let's take advantage of them by creating a simple console application.

After creating a new console application, add a link to the Microsoft.TeamFoundation.Client.dll library, which is part of ReferenceAssemblies version 2.0. The client object model for TFS is almost entirely developed using .NET 3.5 (CLR 2.0) to support the launch of TFS web parts in SharePoint.



The following is sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkCredential netCred = new NetworkCredential(
                "someone@yahoo.com",
                "password");
            BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
            TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
            tfsCred.AllowInteractive = false;
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
                new Uri("https://YourAcct.visualstudio.com/DefaultCollection"),
                tfsCred);
            tpc.Authenticate();
            Console.WriteLine(tpc.InstanceId);
        }
    }
}


I added two using statements, one for System.Net, to get NetworkCredential, and the other for Microsoft.TeamFoundation.Client, to access the TFS classes we need.
First, we will create a standard NetworkCredential object with a username (the email address will be the same as your Microsoft account) and the password that you created for the alternate credentials. In the TfsClientCredentials object, we set the AllowInteractive parameter to false to disable the dialog box when using invalid credentials.
When creating the TfsTeamProjectCollection collection, we must specify the collection URL and credentials. Please note that all connections to TF Service accounts require the use of HTTPS. Currently, each account in TF Service has one collection, so it is always called DefaultCollection.
Finally, we call the Authenticate () method to verify the validity of the specified credentials and test the health of the code, for which a unique InstanceId of this collection is displayed.
Now applications that do not request credential input can take full advantage of the TFS client object model using the TF Service.

New interesting materials on development tools in Russian.



The Russian MSDN team regularly searches for the most interesting English-language materials for translation and publishes them in a special blog blogs.msdn.com/b/developer-tools-rus
Now there are already more than a dozen articles on various topics, maybe some of them will be of interest to you :

Also popular now: