LibGit2Sharp和身份验证用户界面 [英] LibGit2Sharp and Authentication UI

查看:211
本文介绍了LibGit2Sharp和身份验证用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与LibGit2Sharp一起为应用程序添加许多Git操作.我添加了Microsoft.Alm.Authentication来帮助进行身份验证和凭据管理器访问.对于从命令行输入的检索凭据,它非常有用.

不过,有什么方法也可以挂接到凭据管理器的登录UI上,提示输入Github,BitBucket和VSTS的用户名和密码.该用户界面会从命令行自动弹出,但在使用LibGit2Sharp时不会触发.

我已经查看了Github上的GitCredentialManager项目,并且可以看到提供UI的组件,但是在试图弄清楚如何将其显式连接之前,是否有某种方式我会想不到这提供了Microsoft.Alm.Authentication(或相关程序包)的一部分?还是有人可以指出一个示例或指南,以更好地说明这一点?

解决方案

不幸的是,libgit2(或LibGit2Sharp)中没有功能可直接与git-credential-helper功能进行对话,而git本身就是用来执行此操作的. /p>

相反,您可以在PushOptions(或FetchOptions)上设置CredentialsHandler,例如:

options.CredentialsProvider = (url, usernameFromUrl, types) => {
    string username, password;

    Uri uri = new Uri(url);   
    string hostname = uri.Host;

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.UseShellExecute = false;

    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;

    startInfo.FileName = "git.exe";
    startInfo.Arguments = "credential fill";

    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();

    process.StandardInput.WriteLine("hostname={0}", hostname);
    process.StandardInput.WriteLine("username={0}", usernameFromUrl);

    while ((line = process.StandardOutput.ReadLine()) != null)
    {
        string[] details = line.Split('=', 2);
        if (details[0] == "username")
        {
            username = details[1];
        }
        else if (details[0] == "password")
        {
            password = details[1];
        }
    }

    return new UsernamePasswordCredentials(username, password);
};

I'm working with LibGit2Sharp to add a number of Git operations to an application. I've added the Microsoft.Alm.Authentication to help with Authentication and credential manager access. It works great for retrieving credentials that are already entered from the command line.

However is there any way to also hook into the Credential Manager's Login UI that prompts for username and password for Github, BitBucket and VSTS. This UI pops up automatically from the command line, but doesn't fire when using LibGit2Sharp.

I've looked at the GitCredentialManager project on Github and I can see the components that provide the UI, but before trying to figure out how to hook those in explicitly, is there some way I'm missing that this is provided as part of the Microsoft.Alm.Authentication (or related package)? Or can anybody point to an example or guidance on how to best hook this up?

解决方案

Unfortunately, there's no functionality in libgit2 (or LibGit2Sharp) to talk directly to the git-credential-helper functionality, which is what git itself uses to perform this action.

Instead, you can set a CredentialsHandler on your PushOptions (or FetchOptions), eg:

options.CredentialsProvider = (url, usernameFromUrl, types) => {
    string username, password;

    Uri uri = new Uri(url);   
    string hostname = uri.Host;

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.UseShellExecute = false;

    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;

    startInfo.FileName = "git.exe";
    startInfo.Arguments = "credential fill";

    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();

    process.StandardInput.WriteLine("hostname={0}", hostname);
    process.StandardInput.WriteLine("username={0}", usernameFromUrl);

    while ((line = process.StandardOutput.ReadLine()) != null)
    {
        string[] details = line.Split('=', 2);
        if (details[0] == "username")
        {
            username = details[1];
        }
        else if (details[0] == "password")
        {
            password = details[1];
        }
    }

    return new UsernamePasswordCredentials(username, password);
};

这篇关于LibGit2Sharp和身份验证用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆