2012-11-06 40 views
8

我正嘗試從.NET上編寫的Windows服務訪問https://visualstudio.com(以前稱爲https://tfs.visualstudio.com,http://www.tfspreview.com)。如何使用.Net Windows服務中的新基本身份驗證向Visual Studio Team Services進行身份驗證?

我想使用新的基本身份驗證,但我找不到辦法做到這一點。

我發現了很多指向博客文章Team Foundation Service updates - Aug 27的鏈接,但它使用的是適用於TFS的Team Explorer Everywhere Java客戶端。

是否有支持基本認證的TFS .NET對象模型的新版本?

順便說一下,我已經連續登錄服務帳戶。 This answer非常有用。

回答

12

首先,您至少需要在機器上安裝Visual Studio 2012 Update 1。它包括一個更新的Microsoft.TeamFoundation.Client.dll裝配與BasicAuthCredential類。

下面是它的代碼,從Buck's blog post How to connect to Team Foundation Service

using System; 
using System.Net; 
using Microsoft.TeamFoundation.Client; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      NetworkCredential netCred = new NetworkCredential(
       "[email protected]", 
       "yourbasicauthpassword"); 
      BasicAuthCredential basicCred = new BasicAuthCredential(netCred); 
      TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred); 
      tfsCred.AllowInteractive = false; 

      TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
       new Uri("https://YourAccountName.visualstudio.com/DefaultCollection"), 
       tfsCred); 

      tpc.Authenticate(); 

      Console.WriteLine(tpc.InstanceId); 
     } 
    } 
} 
+3

請注意,這需要一個「https:」連接。 – 2014-07-28 20:36:34

+1

我一直有麻煩得到這個工作 - 顯然,當您使用您的Microsoft帳戶憑據提供的值將無法按預期工作。要針對VSO 2015進行身份驗證,我必須啓用「備用憑據」,以便我可以將用戶名更改爲非電子郵件地址格式。之後,此代碼工作正常。 – Volkirith

+0

與Volkirith一致。這不適用於VS2015現在需要先備用憑據設置,然後才能真正運行它。 – afr0

相關問題