How do you authenticate to VSTS using LibGit2Sharp

2019-07-14 05:25发布

I'm trying to clone a VSTS (Visual Studio Team Services) repository using LibGit2Sharp. I'm setting up a CredentialsHandler and UsernamePasswordCredentials representing my Microsoft Account's credentials, and the result I get is this:

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

If I can't even pass in my actual username and password, I'm not sure what might work.

I also tried using DefaultCredentials as mentioned here, but that appears to be only for TFS, not VSTS (NTLM credentials for TFS).

1条回答
迷人小祖宗
2楼-- · 2019-07-14 05:49

First, you need to enable alternate authentication for your account. Follow the steps below to do this:

  1. Login VSTS Web Portal.
  2. Click your account name and select "My profile" from up right corner.
  3. Switch to "Security" panel.
  4. Select "Alternate authentication credentials" and configure it.

enter image description here

Then you can use the alternative credential to authenticate to VSTS. Following code shows how to authenticate to VSTS and perform a clone job.

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);
        }
    }
}
查看更多
登录 后发表回答