我正在尝试获取一个简单的工作示例,用于通过 ssh 克隆或访问远程 git 存储库。添加 nuget package 后LibGit2Sharp-SSH v1.0.22
,得到一个 .Net Framework v4.6.1 控制台应用程序,如下所示:
using LibGit2Sharp;
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string localPath = Path.Combine(Path.GetTempPath(), "Example");
string repoPath = "git@example.server.com:Projects/Project1.git";
Repository.Clone(repoPath, localPath, new CloneOptions() { CredentialsProvider = CredentialsHandler });
}
private static Credentials CredentialsHandler(string url, string username, SupportedCredentialTypes types)
{
var sshDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh");
return new SshUserKeyCredentials()
{
Username = username,
Passphrase = string.Empty,
PublicKey = Path.Combine(sshDir, "id_rsa.pub"),
PrivateKey = Path.Combine(sshDir, "id_rsa")
};
}
}
这返回。'Failed to start SSH session: Unable to exchange encryption keys'
如果我改用
repoPath = "ssh://git@example.server.com:Projects/Project1.git";
然后它抛出Malformed URL 'ssh://git@example.server.com:Projects/Project1.git'
。