libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.16k stars 888 forks source link

"Too many redirects or a uthentication replays" error when using "UsernamePasswordCredentials" credentials #1168

Closed MichaelS12 closed 6 years ago

MichaelS12 commented 9 years ago

I got "Too many redirects or a uthentication replays" error when using "UsernamePasswordCredentials"(with self provided username and password) credentials to do clone or pull request. But it works if I use "DefaultCredentials" credentials. In the git bash I can use the self provided user name and password to clone and pull my repository. Any suggestion is appreciated. Thanks!

The version I am using is LibGit2Sharp.0.21.0.176

ghost commented 8 years ago

I'm also encountering this issue, and it doesn't seem to matter if I use UsernamePasswordCredentials or the DefaultCredentials.

This is the entirety of my code:

Repository.Clone(
    "http://tfs:8080/repo,
    @"c:\git\test", 
    new CloneOptions
    {
        CredentialsProvider = (a, b, c) => new UsernamePasswordCredentials {  Username = "foo", Password = "bar"}
    });

When I then fire up Wireshark and look at what is being sent, it never sends any credentials.

The first request is sent without credentials (as expected), the server responds with 401 Unauthorised, and sends WWW-Authenticate headers (plus Cookies). The second request sends back the cookies, but not the authenticate headers.

I'm using the Nuget package - stable and the prerelease versions have the same issue.

ethomson commented 8 years ago

@willhughes Your URL suggests that you're talking to an on-premises TFS server. Please note that UsernamePasswordCredentials do not support NTLM. NTLM and Kerberos is supported only for DefaultCredentials. If you use UsernamePasswordCredentials then you would need to enable HTTP Basic on your server.

ghost commented 8 years ago

@ethomson Thanks for the fast response. Yep, it's an on-premises TFS instance. I tried both DefaultCredentials (running on a machine that's on the same domain as TFS, and as a user that has rights to the repo) and UsernamePasswordCredentials.

I did check for HTTP Basic being enabled on IIS, I'll go re-verify that. I'll also go grab the Wireshark log of the calls too.

ghost commented 8 years ago

@ethomson Double checked, HTTP Basic is enabled for IIS as a whole, the Team Foundation Server Site, and the tfs application. It also passes Basic in headers.

Here's a trimmed/anonymised set of request/response headers:

Request 1:

GET /tfs/DefaultCollection/_git/repo/info/refs?service=git-upload-pack HTTP/1.1
Connection: Keep-Alive
Pragma: no-cache
Accept: */*
User-Agent: git/1.0 (libgit2 0.24.0)
Host: tfs:8080

HTTP/1.1 401 Unauthorized
Set-Cookie: Tfs-SessionId=1234; path=/
Set-Cookie: Tfs-SessionActive=2016-04-27T08:10:57; path=/
WWW-Authenticate: Basic realm="tfs"
WWW-Authenticate: NTLM

Request 2 (should some kind of authentication - either NTLM or Basic)

GET /tfs/DefaultCollection/_git/repo/info/refs?service=git-upload-pack HTTP/1.1
Connection: Keep-Alive
Pragma: no-cache
Accept: */*
User-Agent: git/1.0 (libgit2 0.24.0)
Host: tfs:8080
Cookie: Tfs-SessionActive=2016-04-27T08:10:57; Tfs-SessionId=1234

HTTP/1.1 401 Unauthorized

Any ideas where to go from here?

I get the exact same headers (excepting the unique bits for session id and sessionactive that TFS sends back) regardless of whether I use DefaultCredentials or UsernamePasswordCredentials.

alustrement commented 8 years ago

I got the same behavior with an on premise TFS 2015 with HTTP or HTTPS url (cetificate is found and valid), UsernamePasswordCredentials or DefaultCredentials.

MustafaJamal commented 8 years ago

I am getting same issue with my on prem TFS 2015. My build is installed on machine which is not logged in with TFS valid user while I am using UsernamePasswordCredentials() and mentioning valid credentials but still getting this error. Please tell me the status? I've also tried with @willhughes's solution but no effect.

mattperkins874 commented 8 years ago

Any movement on this issue? Having the same problem with a TFS Git repo

andrevlins commented 7 years ago

Same problem here.

MustafaJamal commented 7 years ago

Hello guys we are waiting desperately to get this issue fixed. We are facing this issue at our client's end it's very much critical situation for us. Can anyone please guide us or fix this issue?

Thanks in advance!

Bodmoor commented 7 years ago

Same issue connecting to bitbucket... Does it matter that is uses https://**Bodmoor@**... as remote url?

MustafaJamal commented 7 years ago

@ethomson I consulted with TFS Team, following was their reply:

Your logs still do not include any authentication information. For example, here is some of your request info in the XML:

This does not include any information that you are supplying any authentication information to the server at all. I recommend diagnosing your code and LibGit2Sharp in order to find out why your code is not submitting authentication information (or verify that it is sending that information across HTTP).

Can any one guide what's wrong with library? Can anyone guide us to how to fix it ?

maximpashuk commented 7 years ago

Hi all, I have the same issue at TFS 2017 OnPremise, made a little investigation. I think this is because TFS server return http authorization headers in order that libgit2 not understand.

I created an issue at libgit2 repo (https://github.com/libgit2/libgit2/issues/4039), you can see details there.

As a workaround I advise to enable Basic authorization in IIS on TFS website. In my case this made TFS 2017 to return authorization headers in order that libgit2 can understand.

Probably not only libgit2 affected, at stackoverflow I found a report that Fiddler also cannot authenticate at TFS, if authorization headers not in right order: http://stackoverflow.com/questions/40866177/tfs-2017-and-fiddler-authentication-problems

MustafaJamal commented 7 years ago

@maximpashuk I tried your suggestion by enabling Basic authorization in IIS for TFS website, but it didn't work for me.

twaldron commented 7 years ago

Same issue when connecting to a VSTS git repo. using UsernamePasswordCredentials with my vsts credentials

ethomson commented 6 years ago

If you're seeing this when using UsernamePasswordCredentials, then your password is wrong. You probably need to set up a Personal Access Token (PAT) on VSTS. Then use the PAT with UsernamePasswordCredentials.

I'm closing this issue because:

  1. This is not an avenue for support. This is our bug tracker. If you have a problem using LibGit2Sharp and need support, please use the LibGit2Sharp tag on StackOverflow.

  2. This has split into two different bug reports - one around DefaultCredentials with TFS2017 and TFS2018. This is resolved in https://github.com/libgit2/libgit2/pull/4453 and will be resolved in a future LibGit2Sharp update that includes that new version. The other bug is that this is a not obvious error message. I will update the error message to be more clear that this is a problem with the credentials.