【问题标题】:How to validate a credentials provider如何验证凭据提供程序
【发布时间】:2020-05-07 12:40:45
【问题描述】:

给定用户和密码的组合,我想确定这会为已知的远程 git 服务器生成有效的UsernamePasswordCredentials

理想情况下,无论是否发生本地结帐,我都希望能够做到这一点。

我尝试过的是这个。我有一个具有以下方法的类

    private CredentialsHandler UserPassCredentialsProvider
    {
        get
        {
            if (_gitPass == null)
                throw new System.Exception("No git Credentials have been supplied");

            var credentials = new UsernamePasswordCredentials()
            {
                Username = _gitUser,
                Password = _gitPass
            };

            return (_url, _usr, _cred) => credentials;
        }
    } 


    public bool CheckCredentials()
    {
        try
        {  
            using (var repo = new Repository(Path.Combine(WorkingCopyRoot, "MyRepo")))
            {
                IEnumerable<Reference> refs = Repository.ListRemoteReferences(Path.Combine(WorkingCopyRoot, "MyRepo"), UserPassCredentialsProvider);

                return refs != null;
            }
        }
        catch(Exception ex)
        {
            return false;
        }
    }

这只是我在撰写本文时所拥有的代码 - 我已经破解了一段时间,但是,ListRemoteReferences() 似乎在使用或不使用有效密码的情况下都可以正常工作。

实现我想要的东西似乎并不简单 - 有什么建议吗?

【问题讨论】:

    标签: libgit2sharp


    【解决方案1】:

    我已经很接近了。通过将路径从本地切换到远程,我能够让它按我的意愿工作。在下面的代码中,RepoRoot 是一个 URL。

    public bool CheckCredentials()
    {
        try
        {
            IEnumerable<Reference> refs = Repository.ListRemoteReferences(Path.Combine(RepoRoot, "MyRepo"), UserPassCredentialsProvider);
    
            return refs != null;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-11
      • 2013-03-16
      • 2018-11-28
      • 2020-07-22
      • 2021-11-29
      • 2019-04-26
      • 2020-01-11
      • 2019-07-29
      相关资源
      最近更新 更多