gpt4 book ai didi

c# - 什么会导致 git pull(通过 libgit2sharp)出现 401 错误?

转载 作者:行者123 更新时间:2023-12-05 07:54:57 26 4
gpt4 key购买 nike

这是我在本地网络上运行的 Pull 包装器:

internal MergeStatus Pull()
{
using (var repo = new Repository(repositoryRoot))
{
var merger = new Signature(userName,emailAddress,DateTimeOffset.UtcNow);

var options =new PullOptions {
FetchOptions = new FetchOptions()
{
CredentialsProvider = CredentialsHandler
}
};

MergeResult result = repo.Network.Pull(merger, options);
return result.Status;
}
}

以下显示了我正在使用的 CredentialsHandler 以及使用 UsernamePasswordCredentials 的注释掉尝试,这给了我完全相同的结果。

public LibGit2Sharp.Handlers.CredentialsHandler CredentialsHandler 
{
get
{
if (credentialsHandler_ == null)
{
credentialsHandler_ = (_url, _user, _cred) => new DefaultCredentials();

//This does not fix the 401 error on Jupiter box
//credentialsHandler_ = (_url, _user, _cred) => new UsernamePasswordCredentials
//{
// Username = userName,
// Password = pwd,
//};
}
return credentialsHandler_;
}

set
{
credentialsHandler_ = value;
}
}

Pull 功能在本地网络上运行良好,但当我从不同网络上的机器使用它但具有相同的目录结构和 git 存储库的克隆时,我收到此 401 错误:

LibGit2Sharp.LibGit2SharpException: Request failed with status code: 401 at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) at LibGit2Sharp.Core.Proxy.git_remote_fetch(RemoteSafeHandle remote, Signature signature, String logMessage) at LibGit2Sharp.Network.DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, Signature signature, String logMessage) at LibGit2Sharp.Network.Fetch(Remote remote, FetchOptions options, Signature signature, String logMessage) at LibGit2Sharp.Network.Pull(Signature merger, PullOptions options) at GitFace.GitFileListController.Pull() in c:\Code\Workspace\GitFace\GitFileListController.cs:line 190

请注意,当我使用 git bash 并输入“git pull”时,我需要在本地和网络计算机上都输入密码,但都成功地从远程存储库中提取了更改(FWIW,实际存储库在 TFS 上本地网络)。我通过 git bash 进行 pull 和推送的过程在两台计算机上是相同的,因此当 libgit2sharp 代码的行为在一台计算机与另一台计算机上不同时,我感到很惊讶。

注意:这不起作用:

private int GitCredentialHandler(out IntPtr ptr, IntPtr cUrl, IntPtr usernameFromUrl, GitCredentialType credTypes, IntPtr payload)
{
string url = LaxUtf8Marshaler.FromNative(cUrl);
string domain = ConfigurationManager.AppSettings["Domain"];
if (url.StartsWith(@"http://"))
{
url = string.Format(@"http://{0}\\\{1}", domain, url.Substring(7));
}
else if (url.StartsWith(@"https://"))
{
url = string.Format(@"https://{0}\\\{1}", domain, url.Substring(8));
}

string username = string.Format(@"{0}\{1}", domain, LaxUtf8Marshaler.FromNative(usernameFromUrl));
SupportedCredentialTypes types = default(SupportedCredentialTypes);
if (credTypes.HasFlag(GitCredentialType.UserPassPlaintext))
{
types |= SupportedCredentialTypes.UsernamePassword;
}
if (credTypes.HasFlag(GitCredentialType.Default))
{
types |= SupportedCredentialTypes.Default;
}

var cred = CredentialsProvider(url, username, types);

return cred.GitCredentialHandler(out ptr);
}

最佳答案

我更改了我的代码以始终通知用户名和密码(更改为 appsettings/secrets)并且我成功了。

private void FazerPullDeUmRepositorio(Repository repo)
{
string gitUser = "[my.user.name-here]", gitToken = "[my-password-or-token-here]";

var options = new PullOptions
{
FetchOptions = new FetchOptions { CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = gitUser, Password = gitToken } }
};

var email = repo.Config.FirstOrDefault(x => x.Key == "user.email").Value;
var nome = repo.Config.FirstOrDefault(x => x.Key == "user.name").Value;
var usuario = new Signature(nome, email, DateTime.Now);

Logs.AdicionarInfo($"\nPull iniciado em: '{ObterNomeDoRepositorio(repo)}'");
MergeResult resultado;

try
{
resultado = Commands.Pull(repo, usuario, options);
}
catch (LibGit2SharpException ex)
{
if (ex is CheckoutConflictException exConclito)
{
Logs.AdicionarErro($"Temos conflito(s) no repositório {ObterNomeDoRepositorio(repo)}.");
return;
}
else if (ex.Message.Equals("There is no tracking information for the current branch."))
{
Logs.AdicionarSucesso("Branche só existe local.");
return;
}

throw;
}

关于c# - 什么会导致 git pull(通过 libgit2sharp)出现 401 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650478/

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