📜 ⬆️ ⬇️

Connect to the TFS service without entering LiveID credentials

Typically, when connecting to Team Foundation Service, a user sees a webpage for logging in using a Microsoft account, also called LiveID. When logging in, the user can check the box to save their Microsoft account. In this case, it will not have to be re-entered, unless there are large breaks between the input sessions (then the system will request the data again).
This is very convenient for people, but what if you need to connect to an application or web service? To this end, the program code should use "alternative credentials", the use of which must be enabled in the account settings. This is the same parameter that is used when enabling basic authentication for git-tf . You can then write program code that uses these credentials to connect to the service. In the long term, we will also add support for OAuth, but it is still in the experimental stage.


Enable alternate credentials.



First of all, this feature must be enabled. First, open your account or project in your 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.



Use of alternate credentials in code
Before performing the steps below, make sure that Visual Studio 2012 Update 1 or later is installed on your computer. This update includes extensions for 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 ... (Extensions and Updates ...), click Updates, then Product Updates ( Product updates) and install the latest update. This update can also be downloaded here .
To make sure that you have installed Visual Studio with update 1 or a newer version, select Help (Help) -> About Microsoft Visual Studio.



Having completed the configuration of the credentials, let's use 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 included in 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.



Below is a 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 you created for alternate credentials. In the TfsClientCredentials object, we set the AllowInteractive parameter to false to disable the appearance of a dialog box when using invalid credentials.
When creating the TfsTeamProjectCollection collection, we must provide the collection URL and credentials. Please note that all connections with TF Service accounts require the use of the HTTPS protocol. Currently, each account in the TF Service corresponds to one collection, so it is always called DefaultCollection.
Finally, we call the Authenticate () method to verify the specified credentials and test the health of the code, for which the unique identifier InstanceId of this collection is displayed.
Now applications that do not request credentials can use all the features 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 the special blog blogs.msdn.com/b/developer-tools-rus
Now there are more than a dozen articles on various topics, maybe some of them will be interesting to you:

Source: https://habr.com/ru/post/171209/


All Articles