How do you authenticate to VSTS using LibGit2Sharp?

天大地大妈咪最大 提交于 2020-01-14 14:26:05

问题


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:


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.

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);
        }
    }
}


来源:https://stackoverflow.com/questions/36927437/how-do-you-authenticate-to-vsts-using-libgit2sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!