2016-04-29 104 views
0

我想用LibGit2Sharp克隆一個VSTS(Visual Studio Team Services)存儲庫。我設置了一個CredentialsHandlerUsernamePasswordCredentials代表我的Microsoft帳戶的憑據,結果我得到的是這樣的:如何使用LibGit2Sharp對VSTS進行身份驗證?

LibGit2Sharp.LibGit2SharpException was unhandled 
    HResult=-2146233088 
    Message=Too many redirects or authentication replays 
    Source=LibGit2Sharp 

如果我甚至無法通過我的實際用戶名和密碼,我不知道是什麼可能會工作。

我也嘗試使用DefaultCredentials作爲here,但似乎只用於TFS,而不是VSTS(TFS的NTLM憑據)。

回答

4

首先,您需要爲您的帳戶啓用備用驗證。請按照以下步驟操作:

  1. 登錄VSTS Web Portal。
  2. 點擊您的帳戶名稱,然後從右上角選擇「我的個人資料」。
  3. 切換到「安全」面板。
  4. 選擇「備用認證憑證」並對其進行配置。

enter image description here

然後你可以使用替代憑據來驗證VSTS。以下代碼顯示瞭如何向VSTS進行身份驗證並執行克隆作業。

using LibGit2Sharp; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string surl = "https://youraccount.visualstudio.com/DefaultCollection/_git/Remoterepo"; 
      string lpath = "C:\\LocalPath"; 
      CloneOptions co = new CloneOptions(); 
      string un = "alternativeusername"; 
      string pwd = "alternativepassword"; 
      Credentials ca = new UsernamePasswordCredentials() {Username = un, Password = pwd }; 
      co.CredentialsProvider = (_url, _user, _cred) => ca ; 
      Repository.Clone(surl,lpath,co); 
     } 
    } 
} 
+0

太棒了。我實際上已經進入了這個領域並且完成了一切正確的事情,除了我曾經認爲我仍然可以使用我的主要電子郵件地址和密碼,並且現在只勾選了複選框。看起來你真的必須輸入一個輔助用戶名來完成這項工作。 你知道Windows的Git Credential Manager(https://github.com/Microsoft/Git-Credential-Manager-for-Windows)是否允許使用常規憑證嗎? – Todd