2012-12-07 17 views
5

Github'a bağlanmak için NGit kullanmaya çalışıyorum (örn. Özel bir anahtar ve parola kullanarak).Özel anahtar dosyası ile bağlantı oluşturma NGT

Birisi bana katılabilir mi?

Benim normal olacağını getirme:

  var git = Git.CloneRepository() 
      .SetDirectory(_properties.OutputPath) 
      .SetURI(_properties.SourceUrlPath) 
      .SetBranchesToClone(new Collection<string>() { "master" }) 
      .SetCredentialsProvider(new UsernamePasswordCredentialsProvider("username","password")) 
      .SetTimeout(3600) 
      .Call(); 

nasıl bu özel anahtarla yapardınız?

cevap

4

Uzun bir savaştan sonra, Jgit örnekleri için çok fazla google araması ve çok daha fazla acı, çözümü buldum!

Temel olarak geçersiz kılmak ve kendi ile SessionFactory ve bağlantı zaman sertifika enjekte: o zaman

public class CustomConfigSessionFactory : JschConfigSessionFactory 
{ 
    public string PrivateKey { get; set; } 
    public string PublicKey { get; set; } 

    protected override void Configure(OpenSshConfig.Host hc, Session session) 
    { 
     var config = new Properties(); 
     config["StrictHostKeyChecking"] = "no"; 
     config["PreferredAuthentications"] = "publickey"; 
     session.SetConfig(config); 

     var jsch = this.GetJSch(hc, FS.DETECTED); 
     jsch.AddIdentity("KeyPair", Encoding.UTF8.GetBytes(PrivateKey), Encoding.UTF8.GetBytes(PublicKey), null); 
    } 
} 

Ve böylece gibi enjekte:

var customConfigSessionFactory = new CustomConfigSessionFactory(); 
customConfigSessionFactory.PrivateKey = properties.PrivateKey; 
customConfigSessionFactory.PublicKey = properties.PublicKey; 

NGit.Transport.JschConfigSessionFactory.SetInstance(customConfigSessionFactory); 

var git = Git.CloneRepository() 
      .SetDirectory(properties.OutputPath) 
      .SetURI(properties.SourceUrlPath) 
      .SetBranchesToClone(new Collection<string>() { "master" }) 
      .Call(); 
+1

Hızlı ipucu: Bir GIT_SSH ortam değişkeni varsa set, Jsch, SSH bağlantısını başlatırken yapılandırılmış JschConfigSessionFactory kullanmak yerine kullanacaktır. Benim durumumda, TortoisePlink.exe çağırır. –

+0

NGit için GIT_SSH env değişkeni hakkında başka bir soru oluşturursanız, bu sorunun kapsamına bir cevap vermem gerekir. – Doug

İlgili konular