How to provide credentials to Fetch() and Pul() me

2019-06-01 19:24发布

问题:

I'm looking for a nice clean example of how to do a pull using lidgit2sharp. I took the example here which does a fetch, but the reference to Credentials is throwing an exception.

using (var repo = new Repository(workingDir, null))
{
    Remote remote = repo.Network.Remotes.Add("master", repoUrl);
    repo.Network.Fetch(remote, 
    {    
        Credentials credentials = new Credentials
        {
            Username = "username",
            Password = "password"
        }
    });
}

The exception is Cannot create an instance of the abstract class or interface 'LibGit2Sharp.Credentials'

回答1:

It's telling you in the error: Credentials is an abstract class, interface, whatever.

Try this:

Credentials credentials = new UsernamePasswordCredentials()
{

        Username = "username",
        Password = "password"
}