gpt4 book ai didi

c# - LibGit2Sharp 和身份验证 UI

转载 作者:行者123 更新时间:2023-11-30 15:16:17 26 4
gpt4 key购买 nike

我正在使用 LibGit2Sharp 向应用程序添加一些 Git 操作。我添加了 Microsoft.Alm.Authentication 以帮助进行身份验证和凭据管理器访问。它非常适合检索已从命令行输入的凭据。

但是,还有什么方法可以连接到提示输入 Github、BitBucket 和 VSTS 的用户名和密码的凭据管理器的登录 UI。此 UI 从命令行自动弹出,但在使用 LibGit2Sharp 时不会触发。

我查看了 Github 上的 GitCredentialManager 项目,我可以看到提供 UI 的组件,但在尝试弄清楚如何显式地 Hook 这些组件之前,我是否遗漏了一些方法,这是作为Microsoft.Alm.Authentication(或相关包)的一部分?或者任何人都可以指出有关如何最好地连接它的示例或指南吗?

最佳答案

我设法让您的解决方案通过一些小的修改工作。我在此处粘贴示例代码以使用 git 凭据进行推送。它使用已存储在计算机中的凭据工作,并在第一次使用 UI 时提示输入凭据。

到目前为止,我遇到的唯一问题是提示用户输入凭据并且他们输入了无效的用户名/密码。 Git 向控制台写入请求用户/密码,并且在您输入之前该过程不会完成。试图监控 StandardError/Output 但没有成功。我在 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);
}
}

关于c# - LibGit2Sharp 和身份验证 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50010941/

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