我正在使用 LibGit2Sharp 向应用程序添加许多 Git 操作。我已经添加了Microsoft.Alm.Authentication
帮助进行身份验证和凭证管理器访问。它非常适合检索已从命令行输入的凭据。
但是,有什么方法可以连接到凭据管理器的登录 UI,提示输入 Github、BitBucket 和 VSTS 的用户名和密码。此 UI 会从命令行自动弹出,但在使用 LibGit2Sharp 时不会触发。
我查看了 Github 上的 GitCredentialManager 项目,我可以看到提供 UI 的组件,但在尝试弄清楚如何显式挂钩这些组件之前,我是否遗漏了一些方法,即这是作为Microsoft.Alm.Authentication
(或相关包)?或者有人可以指出如何最好地将其连接起来的示例或指南吗?
我设法通过一些小的修改让您的解决方案正常工作。我在此处粘贴了使用 git 凭据进行推送的示例代码。它使用已存储在计算机中的凭据进行工作,并在第一次使用 UI 时提示输入凭据。
到目前为止,我遇到的唯一问题是当用户提示输入凭据并且他们输入无效的用户/密码时。 Git 向控制台写入请求用户名/密码,直到您输入该信息后,该过程才完成。尝试监视标准错误/输出但没有成功。我在 stderror 中收到错误文本,但只有在手动填写后才得到。
public void PushLibGit2Sharp(string repositoryFolder, string branch)
{
using (var repo = new Repository(repositoryFolder))
{
var options = new PushOptions
{
CredentialsProvider = (url, usernameFromUrl, types) =>
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "git.exe",
Arguments = "credential fill",
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
Process process = new Process
{
StartInfo = startInfo
};
process.Start();
// Write query to stdin.
// For stdin to work we need to send \n instead of WriteLine
// We need to send empty line at the end
var uri = new Uri(url);
process.StandardInput.NewLine = "\n";
process.StandardInput.WriteLine($"protocol={uri.Scheme}");
process.StandardInput.WriteLine($"host={uri.Host}");
process.StandardInput.WriteLine($"path={uri.AbsolutePath}");
process.StandardInput.WriteLine();
// Get user/pass from stdout
string username = null;
string password = null;
string line;
while ((line = process.StandardOutput.ReadLine()) != null)
{
string[] details = line.Split('=');
if (details[0] == "username")
{
username = details[1];
}
else if (details[0] == "password")
{
password = details[1];
}
}
return new UsernamePasswordCredentials()
{
Username = username,
Password = password
};
}
};
repo.Network.Push(repo.Branches[branch], options);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)