Search code examples
gittfslibgit2libgit2sharp

Authorization with TFS PAT in libgit2sharp


I'd like to access in-premise TFS server GIT repositories using LibGit2Sharp. So far I'm only playing around with this library, and I can't get it to work as I'd expect it would.

My first problem is authorization with Personal Access Tokens. I can clone a repository using UsernamePasswordCredentials providing my company email and password, however, I can't find a way to do so with PAT. I can't find any reliable information about it, so I tried using it in different combinations, like replacing my password/username with it, but I can't get it to work - I always get LibGit2Sharp.LibGit2SharpException: 'too many redirects or authentication replays'.

It's supposed to mean credentials are invalid, but I'm sure my token is valid and I authorized all scopes, so it should be able to do everything I am able to with my credentials. Is there an example I could follow to get it up and running? Here's my current solution with username and password that works:

        var co = new CloneOptions();
        co.CredentialsProvider = (_url, _user, _cred) =>
            new UsernamePasswordCredentials
            {
                Username = "[email protected]",
                Password = "password"
            };

        Repository.Clone(@"remoteUrl-received-from-api", "destination-folder", co);

My other question is, which URL received from this API endpoint: TFS API is the correct one to pass to LibGit2Sharp? I'm using the "remoteUrl"(which is the TFS web app URL) to clone and it works when provided with username and password, however, Repository.IsValid returns false. I'm not that familiar with TFS and GIT so please forgive my complete lack of knowledge in this area.

If I don't get this working tomorrow I'll have to use a cmd.exe process and console commands, which I would really like to avoid - so any help would be really apprecieated.


Solution

  • In libgit2sharp, seems passing the token as the username with a blank password is specific to GitHub. This may not works well with TFS/VSTS hosted git repo.

    Take a look at this related question in GITHUB: Does Git-Clone support Personal access tokens ?

    For TFS/VSTS, if you don't want to use UsernamePasswordCredentials providing your company email and password, you could use Alternate authentication credentials instead.

    enter image description here

    For the url, you should use the TFS git remoterepo url. More detail sample please refer this link: How do you authenticate to VSTS using LibGit2Sharp?

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