gpt4 book ai didi

c# - NGit 与私钥文件建立连接

转载 作者:太空狗 更新时间:2023-10-29 13:23:34 24 4
gpt4 key购买 nike

我正在尝试使用 NGit 连接到 Github(即使用私钥和密码)。

有人可以指导我完成它吗?

我的正常获取是:

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

我如何使用私钥执行此操作?

最佳答案

经过长时间的战斗,许多谷歌搜索 Jgit 示例和更多的痛苦,我找到了解决方案!

基本上你用自己的覆盖和 SessionFactory 并在连接时注入(inject)你的证书:

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);
}
}

然后像这样注入(inject)它:

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();

关于c# - NGit 与私钥文件建立连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13764435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com