NGit (JGit port) authentication with SSH on C#

746 views Asked by At

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://[email protected]:xxxx/repo.git");
pshCmd.SetTransportConfigCallback(new NTransportConfigCallBack());
pshCmd.Call();

Following exception on PushCommand.Call():

"TransportException: ssh://[email protected]: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.

1

There are 1 answers

0
RĂ¼diger Herrmann On BEST ANSWER

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

defaultJsch.AddIdentity( "<path>", "<passphrase>" )