Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the new CredentialsProvider in LibGit2Sharp?

I using the LibGit2Sharp.Credentials class for some time at the following way:

LibGit2Sharp.Credentials credentials = new UsernamePasswordCredentials()
{
       Username = TokenValue,
       Password = ""
};

var pushOptions = new PushOptions() { Credentials = credentials} ;

Now the LibGit2Sharp.PushOptions.Credentials is obsolate, I have to use CredentialsProvider.

I want to ask you what is the correct way to use CredentialsProvider in this case?

Thank you very much!

like image 511
Gábor Domonkos Avatar asked Mar 18 '23 09:03

Gábor Domonkos


1 Answers

>I want to ask you what is the correct way to work with CredentialsProvider at this case?

This piece of code should fit your need.

var pushOptions = new PushOptions() { 
    CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials
    {
        Username = TokenValue,
        Password = ""
    }
}

This has been introduced by PR #761 in order to allow some more interactive scenarios (when the user is being asked for his credentials as part of the clone process, for instance) and prepare the path for other kinds of credentials (Ssh, for instance).

The CredentialsProvider is a callback which

  • passes in the url being targeted, the username (if it was specified in the url) and the type of credentials the server accepts
  • expects in return a Credentials (base type) which will be used during the authentication

One can see an example of the CredentialsProviders in action in the CloneFixture.cs test suite.

like image 180
nulltoken Avatar answered Mar 21 '23 07:03

nulltoken