2013-04-26 86 views
1

会话部分与私钥的连接没有问题。但是,当我做一个git克隆,它会给出错误'验证失败'。如何包装,绑定或使连接的会话与git克隆一起工作。我在.NET 4.0下使用NGIT,但不认为这很重要,因为JGIT几乎是一样的。NGIT/JGIT/Git#使用私钥的SSH会话克隆GiT存储库

任何想法?这里

感谢加文

 JSch jsch = new JSch(); 
     Session session = jsch.GetSession(gUser, gHost, 22); 
     jsch.AddIdentity(PrivateKeyFile); // If I leave this line out, the session fails to Auth. therefore it works. 
     Hashtable table = new Hashtable(); 
     table["StrictHostKeyChecking"] = "no"; // this works 
     session.SetConfig(table); 
     session.Connect(); // the session connects. 



     URIish u = new URIish(); 
     u.SetPort(22); 
     u.SetHost(gHost); 
     u.SetUser(gUser);    
     NGit.Transport.JschSession jschSession = new JschSession(session,u); 

     if (session.IsConnected()) 
     { 
      try 
      { 
       CloneCommand clone = Git.CloneRepository() 
        .SetURI(gitAddress) 
        .SetDirectory(folderToSave);           
       clone.Call();     

      // MessageBox.Show(Status, gitAddress, MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
      catch (Exception ex) 
      { 
       // AUth Fail..... ???? 

      } 
     } 
     else 
     { 
      session.Disconnect(); 

     } 
     session.Disconnect(); 
+0

我在凭证提供程序中添加以查看它是否有帮助。 ()) CloneCommand clone = Git.CloneRepository() .SetCredentialsProvider(new CustomCredentialsProvider()) .SetURI(gitAddress) .SetDirectory(folderToSave); clone.Call(); – Gavin 2013-04-29 08:12:38

+0

您使用的是哪个版本的NGit?我注意到的一件事是,从NuGet获得的NGit相当老旧(2011年),因此可能会在修复此问题的错误修复方面落后。就我而言,我根本无法连接,但随后将我的NGit版本更新为[最新来自github](https://github.com/mono/ngit)。 – 2013-11-30 07:38:03

回答

1

的问题是会话对象实际上并非在任何时候CloneCommand相关。因此,您为完成会话所做的所有工作都没有做任何事情,因为CloneCommand将自己创建自己的会话(使用默认会话项目)。

克隆命令将从SSHSessionFactory获得实际使用的会话。首先,你需要创建一个实现SSHSessionFactory抽象类的类,像我下面做:

public class MySSHSessionFactory : SshSessionFactory 
{ 
    private readonly JSch j; 

    public MySSHSessionFactory() 
    { 
     this.j = new JSch(); 
    } 

    public void Initialize() 
    { 
     this.j.SetKnownHosts(@"C:/known_hosts"); 
     this.j.AddIdentity(@"C:\id_rsa"); 
    } 

    public override RemoteSession GetSession(URIish uri, CredentialsProvider credentialsProvider, NGit.Util.FS fs, int tms) 
    { 
     var session = this.j.GetSession(uri.GetUser(), uri.GetHost()); 
     session.SetUserInfo(new MyUserInfo()); 
     session.Connect(); 

     return new JschSession(session, uri); 
    } 
} 

然后你就可以将所有新的Git命令使用此工厂时,他们希望使用一个会话:

var sessionFactory = new MySSHSessionFactory(); 
sessionFactory.Initialize(); 
SshSessionFactory.SetInstance(sessionFactory); 

// Now you can do a clone command. 

请注意,我仍然没有搞清楚这个库,所以我会以最佳的方式还没有写MySSHSessionFactory(它处理其关闭,例如会话容错?)。但这至少是一个开始。

+0

感谢马克,这里是另一个例子下,我使用这个技巧来启用一个http代理设置,同时使用JGit与“ssh://”git存储库url形式(通过http代理的jgit ssh)http:// goo。 GL/SVqQ5l – boly38 2013-12-04 16:53:43