您如何使用LibGit2Sharp向VSTS进行身份验证?

Tod*_*odd 2 git tfs libgit2sharp azure-devops

我正在尝试使用LibGit2Sharp克隆VSTS(Visual Studio Team Services)存储库。我正在设置CredentialsHandlerUsernamePasswordCredentials代表我的Microsoft帐户的凭据,得到的结果是:

LibGit2Sharp.LibGit2SharpException was unhandled
  HResult=-2146233088
  Message=Too many redirects or authentication replays
  Source=LibGit2Sharp
Run Code Online (Sandbox Code Playgroud)

如果我什至无法传递我的真实用户名和密码,我不确定是否可行。

我也尝试过使用这里DefaultCredentials提到的方法,但这似乎仅用于TFS,而不是VSTS(用于TFS的NTLM凭据)。

Edd*_*SFT 5

首先,您需要为您的帐户启用备用身份验证。请按照以下步骤进行操作:

  1. 登录VSTS Web门户。
  2. 点击您的帐户名称,然后从右上角选择“我的个人资料”。
  3. 切换到“安全性”面板。
  4. 选择“备用身份验证凭据”并进行配置。

在此处输入图片说明

然后,您可以使用备用凭据对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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)