【问题标题】:Azure Devops OnPremise, fatal: Authentication failed for when cloning Git repoAzure Devops OnPremise,致命:克隆 Git 存储库时身份验证失败
【发布时间】:2020-10-24 05:47:15
【问题描述】:

使用 OnPremise Azure Devops 2019 服务器我正在尝试通过在 ProcessInfo 中调用以下 git 命令以编程方式克隆 git 存储库:

clone https://{username}:{PATTOKEN}@{DEVOPSSERVERDOMAIN}/{TEAMPROJECT}/_git/{PROJECT}

这里是c#代码:

  var gitInfo = new ProcessStartInfo
                    {
                        CreateNoWindow = true,
                        RedirectStandardError = true,
                        RedirectStandardOutput = true,
                        FileName = pathToGitExe
                    };

Process gitProcess = new Process();
                gitInfo.Arguments = theCloneCommand
                gitInfo.WorkingDirectory = workingFolder;
                gitProcess.StartInfo = gitInfo;
                gitProcess.Start();

但我不断收到错误

Cloning into 'PROJECT'...
fatal: Authentication failed for '{DEVOPSSERVERDOMAIN}/{TEAMPROJECT}/_git/{PROJECT}'

是否未正确使用 PAT?任何建议

【问题讨论】:

  • 嗨,您有没有机会尝试将额外的标头添加到 git 命令中,如下面的答案所述,效果如何?
  • 嗨@LeviLu-MSFT 谢谢!我正在测试它,今天我会告诉你!

标签: c# git azure-devops


【解决方案1】:

如果为 Azure Devops 服务器启用 IIS 基本身份验证,则 PAT 无效。见Enabling IIS Basic Authentication invalidates using Personal Access Tokens

正如上面文档中所说,您需要在 Git 请求中添加一个额外的标头,其中包含“user:PAT”的 base 64 编码:

git -c http.extraheader='Authorization: Basic [base 64 encoding of "user:PAT"]' ls-remote http://tfsserver:8080/tfs/DefaultCollection/_git/projectName

您可以使用以下 poweshell 脚本对用户名和 PAT 进行 Base64 编码:

$MyPat = 'yourPAT'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("user:$MyPat"))

或使用 C# 代码:

Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "user", personalaccesstoken))));

有关详细信息,请参阅文档here

您可以尝试使用您的用户名和密码而不是 PAT 作为身份验证:

git clone https://{username}:{password}@{DEVOPSSERVERDOMAIN}/{TEAMPROJECT}/_git/{PROJECT}
#if your password or username contain @ replace it with %40

【讨论】:

  • 嗨 @LeviLu-MSFT 我用 http.extraheader 试过了,所以 gitInfo.Arguments = "-c http.extraheader='Authorization:Basic TheEncodedPAT' ls-remote TheServerUrl" 但我得到了以下错误 git: 'TheEncodedPAT'' 不是 git 命令。请参阅“git --help”。
  • 使用 processinfo 参数和字符串属性可能存在问题
【解决方案2】:

据我所知,您应该尝试不使用username 的克隆。

https://github.com/MicrosoftDocs/azure-devops-docs/issues/2455

https://<PAT>@mydomain.visualstudio.com/myproject/_git/myrepo

【讨论】:

  • 嗨@AlexAIT 我尝试了你的建议,但我收到了致命错误:无法访问“UrlToProject”:SSL 证书问题:无法获取本地颁发者证书
  • 对我来说感觉像是向前迈进了一步。此问题通常指向无法验证的自签名证书。在这种情况下,有一些解决方法,例如禁用验证,例如:confluence.atlassian.com/bitbucketserverkb/… 如果您专门查找此错误,则可以在线获取更多信息。
  • 我最终设置了这里建议的 sChannel 属性:stackoverflow.com/questions/23885449/… 所以不需要 PAT 只需调用命令 git config --global http.sslbackend schannel
猜你喜欢
  • 2022-10-17
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多