How can I skip SSL verification using libgit2sharp?

旧巷老猫 提交于 2019-12-12 03:23:26

问题


I am new to libgit2sharp (and git in general). I am trying to write a small program that handles multiple repositories in an unified manner. However, I've hit a brick wall when trying to deactivate the SSL verification. The library doesn't seem to provide an easy way to do this. I've found a branch (which seems to deal with the problem) and copied some code from there

My current code looks like this (I've deleted personal stuff"):

static void Main(string[] args)
        {
//Additional code
            RemoteCertificateValidationCallback certificateValidationCallback = (sender, certificate, chain, errors) => { return true; };
            ServicePointManager.ServerCertificateValidationCallback = certificateValidationCallback;
           GlobalSettings.RegisterSmartSubtransport<MockSmartSubtransport>("https");
//Additional code end
          //  Repository rep = new Repository(@"workingdirpath");
            Repository.Clone("url.git", "workingdirpath", new CloneOptions
            {
                CredentialsProvider = new CredentialsHandler(getCredentials),
                Checkout = true,
            });
            //Remote remote = rep.Network.Remotes.Add("master", "url.git");
            //rep.Network.Pull(new Signature("me", "me@company.de", new DateTimeOffset(DateTime.Now.AddDays(-2))), new PullOptions
            //    {
            //        FetchOptions = new FetchOptions { CredentialsProvider = new CredentialsHandler(getCredentials), TagFetchMode = TagFetchMode.Auto },
            //    });


        }

        private static Credentials getCredentials(string url, string usernameFromUrl, SupportedCredentialTypes types)
        {
            return new UsernamePasswordCredentials
            {
                Username = "myaccount",
                Password = "mypass",
            };
        }

The "MockSmartSubtransport" class can be found in the linked branch.

My problem is that, the code seems to work(it seems to disable the SSL verification), but I still get a "{"The remote server returned an error: (401) Unauthorized."}" exception from the server. Without the additional code I get: " {"user cancelled certificate check: "}" from the server.

After debugging the thing it seems to me like the delegate for my credentials never gets called (although it gets registered for callback, when trying with either of the operations). I've checked that the username/password are correct by logging in on the server via Chrome and by committing with TortoiseGit.

At the moment I'm not sure that the SSL is still the problem or something else altogether.

Any ideas? If I am not specific enough about the problem I will try to give more info (please ask).


回答1:


After debugging the thing it seems to me like the delegate for my credentials never gets called

No. They don't, because you've gone and registered your own subtransport. This is not something that should be done without knowing exactly what you're doing. This is not for trivial little things like SSL certificates, this is for when you have your own complete, existing HTTP stack that you want to use and you simply can't use the one that's included in LibGit2Sharp.

If you're experimenting thinking that maybe using your own subtransport will solve a very simple problem then it won't. And pulling a test out and trying to make it actually work is likely to fail.

If you need to turn off SSL verification then use the http.sslVerify configuration setting.



来源:https://stackoverflow.com/questions/28232877/how-can-i-skip-ssl-verification-using-libgit2sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!