LibGit2Sharp: Fetching fails with "Too many redirects or authentication replays"
Asked Answered
F

4

5

Here's the code I'm using to fetch:

public static void GitFetch()
{
    var creds = new UsernamePasswordCredentials()
                {Username = "user",
                 Password = "pass"};
    var fetchOpts = new FetchOptions {Credentials = creds};
    using (repo = new Repository(@"C:\project");)
    {
        repo.Network.Fetch(repo.Network.Remotes["origin"], fetchOpts);
    }
}

but it fails during fetch with the following exception:

LibGit2Sharp.LibGit2SharpException: Too many redirects or authentication replays
Result StackTrace:  
at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_remote_fetch(RemoteSafeHandle remote, Signature signature, String logMessage)
   at LibGit2Sharp.Network.DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, Signature signature, String logMessage)
   at LibGit2Sharp.Network.Fetch(Remote remote, FetchOptions options, Signature signature, String logMessage)

I have verified that the config file has the required remote name and that git fetch works from the command line. I found that the exception originates from libgit2\src\transport\winhttp.c but I couldn't come up with a workaround/solution.

Factoring answered 1/10, 2014 at 15:33 Comment(1)
I get this error when authenticating to an Azure DevOps hosted git repo and I accidentally use my regular credentials rather than a Personal Access Token. I know this isn't the OP's issue, but I wanted to leave a note in case it helps a fellow Googler (most likely myself in ~6 months).Proteolysis
F
9

I tried @Carlos' suggestion in the following way:

public static void GitFetch()
{
    var creds = new UsernamePasswordCredentials()
                {Username = "user",
                 Password = "pass"};
    CredentialsHandler credHandler = (_url, _user, _cred) => creds;
    var fetchOpts = new FetchOptions { CredentialsProvider = credHandler };
    using (repo = new Repository(@"C:\project");)
    {
        repo.Network.Fetch(repo.Network.Remotes["origin"], fetchOpts);
    }
}

I could fetch from public repos on github as well as from password protected private repos on bitbucket; however, I couldn't do the same for the repositories hosted over LAN at work. Turns out they were configured in a way which does not accept UsernamePasswordCredentials provided by libgit2sharp. The following modification allowed me to fetch from repositories over LAN:

    CredentialsHandler credHandler = (_url, _user, _cred) => new DefaultCredentials();

(I'm trying to find out what is the exact difference between the two; if I get further insight into it, I'll update the answer.)

Factoring answered 3/10, 2014 at 10:39 Comment(2)
The difference is that one authenticates with the given username and password, the DefaultCredentials authenticates with your logged in credentials. UsernamePasswordCredentials should work with Basic and NTLM. DefaultCredentials should work with NTLM and Kerberos.Expugnable
CredentialsHandler credHandler = (_url, _user, _cred) => creds; credHandler variable should be named ch in order for code to work, but thanks for answer!!!Derringdo
D
2

The shim that should make the Credentials option work is currently buggy (and is deprecated anyway), pass a CredentialsProvider instead as a callback.

Deth answered 1/10, 2014 at 16:1 Comment(1)
@RovinBhandari What version are you using? Can you reproduce this behavior with other repositories as well? Where are you fetching from?Goles
R
0

This seems to be a very common error message.

We were getting it on pushes to GitHub, because credentials were disabled for security:

https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/

We've solved it by enabling SAML SSO and doing the push outside the C# code, but perhaps using SSH keys somehow with the library or personal access tokens fixes the problem too.

Ricarda answered 20/1, 2022 at 13:10 Comment(0)
V
0

I solve this with instructions below

    // Step 3: Commit changes in repo
    using (var repo = new Repository(destinationDirectory))
    {
        // Stage all changes
        Commands.Stage(repo, "*");

        // Create the commit
        var author = new Signature("User_Name", "User_Email", DateTimeOffset.Now);
        var committer = author;
        var commit = repo.Commit("Commit message", author, committer);


         //Push the commit to the remote repository
        var remote = repo.Network.Remotes["origin"];

        var pushOptions = new PushOptions
            {
                CredentialsProvider = new CredentialsHandler(
                        (url, usernameFromUrl, types) =>
                            new UsernamePasswordCredentials()
                            {
                                    Username = "x-access-token",
                                    Password = "YOUR_PERSONAL_ACCESS_TOKEN (instructions below to genreate)"
                            }
                    ),
            };

        repo.Network.Push(remote, @"refs/heads/main", pushOptions);
    }
  1. Go to the GitHub website and log in to your account.

  2. Click on your profile picture in the top-right corner, and then click on "Settings".

  3. In the left sidebar, click on "Developer settings".

  4. In the left sidebar, click on "Personal access tokens".

  5. Click on the "Generate new token" button.

  6. Provide a descriptive note for the token to identify its purpose.

  7. Under "Select scopes", choose the appropriate permissions for the token based on the actions you need to perform, such as repository access, commit creation, etc.

  8. Click on the "Generate token" button.

  9. GitHub will generate a new Personal Access Token for you. Copy the token value.

  10. Replace "YOUR_PERSONAL_ACCESS_TOKEN" in the code snippet with the actual token you copied.

Vambrace answered 6/7, 2023 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.