1

I'm tyring to setup Git functionality in my application with NGit. The repository is an SSH authenticated one on BitBucket, that I am unable to authenticate for Git functionality like Push, Pull, Clone etc.

My code is as follows:

JschConfigSessionFactory:

public class NSessionFactory : JschConfigSessionFactory
{
    protected override void Configure(OpenSshConfig.Host hc, Session session)
    {
        session.SetConfig("StrictHostKeyChecking", "no");
    }

    protected override JSch CreateDefaultJSch(FS fs)
    {
        JSch defaultJSch = new JSch();
        defaultJSch.SetKnownHosts(@"<path>\known_hosts");
        defaultJSch.AddIdentity(@"<path>\id_rsa");
        return defaultJSch;
    }
}

TransportConfigCallback:

public class NTransportConfigCallBack : TransportConfigCallback
{
    public void Configure(Transport transport)
    {
        SshTransport sshTransport = (SshTransport)transport;
        sshTransport.SetSshSessionFactory(new NSessionFactory());
    }
}

Push command:

Git gitObj = NGit.Api.Git.Open(<path to git repo>);

PushCommand pshCmd = gitObj.Push();
pshCmd.SetRemote("ssh://git@stash.name.com:xxxx/repo.git");
pshCmd.SetTransportConfigCallback(new NTransportConfigCallBack());
pshCmd.Call();

Following exception on PushCommand.Call():

"TransportException: ssh://git@stash.name.com:xxxx/repo.git: Auth fail"

The BitBucket account already has the SSH key setup and everything works with other GIT tools.

At this point I have no idea what could be wrong, and I'd appreciate any pointers in the right direction.

Using C# on Visual Studio 2017 with .Net 4.5.2

EDIT 1

Changing up the code as in the answer here produces the same exception.

PhoenixDev
  • 746
  • 2
  • 9
  • 22
  • Does your remote URL have the right format? It seems that for public BitBucket repositories it would be `ssh://git@bitbucket.org//repo.git`. Can you connect to the repository with CLI Git? – Rüdiger Herrmann Jul 13 '17 at 04:55
  • The URL is correct, and I have already used the same to clone the repository. See here: http://imgur.com/a/JhW02 I'm using the same url in my code. Really don't think that's the problem :( – PhoenixDev Jul 14 '17 at 07:34
  • It seems that your ssh key requires a passphrase, in this case, use `defaultJsch.addIdentity( "", "" )`. – Rüdiger Herrmann Jul 14 '17 at 09:58
  • Ah that was it! Thanks for the tip Rüdiger! – PhoenixDev Jul 17 '17 at 11:20

1 Answers1

1

It seems that your SSH key requires a passphrase, in this case, use

defaultJsch.AddIdentity( "<path>", "<passphrase>" )
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79