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

2023-12-24

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

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

如果我什至无法输入我的实际用户名和密码,我不确定什么可行。

我也尝试过使用DefaultCredentials如上所述here http://quabr.com/33612253/libgit2sharp-and-tfs-git-repository,但这似乎仅适用于 TFS,而不适用于 VSTS(TFS 的 NTLM 凭据)。


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

  1. 登录 VSTS 门户网站。
  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);
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 LibGit2Sharp 向 VSTS 进行身份验证? 的相关文章

随机推荐