您如何使用LibGit2Sharp向VSTS进行身份验证? [英] How do you authenticate to VSTS using LibGit2Sharp?
问题描述
我正在尝试使用LibGit2Sharp克隆VSTS(Visual Studio Team Services)存储库.我正在设置代表我的Microsoft帐户凭据的CredentialsHandler
和UsernamePasswordCredentials
,得到的结果是:
I'm trying to clone a VSTS (Visual Studio Team Services) repository using LibGit2Sharp. I'm setting up a CredentialsHandler
and UsernamePasswordCredentials
representing my Microsoft Account's credentials, and the result I get is this:
LibGit2Sharp.LibGit2SharpException was unhandled
HResult=-2146233088
Message=Too many redirects or authentication replays
Source=LibGit2Sharp
如果我什至无法输入我的真实用户名和密码,我不确定什么可行.
If I can't even pass in my actual username and password, I'm not sure what might work.
我还尝试使用DefaultCredentials
,如此处所述,但似乎仅适用于TFS,而不适用于VSTS(适用于TFS的NTLM凭据).
I also tried using DefaultCredentials
as mentioned here, but that appears to be only for TFS, not VSTS (NTLM credentials for TFS).
推荐答案
首先,您需要为您的帐户启用备用身份验证.请按照以下步骤进行操作:
First, you need to enable alternate authentication for your account. Follow the steps below to do this:
- 登录VSTS Web门户.
- 点击您的帐户名称,然后从右上角选择我的个人资料".
- 切换到安全"面板.
- 选择备用身份验证凭据"并进行配置.
然后,您可以使用备用凭据对VSTS进行身份验证.以下代码显示了如何向VSTS进行身份验证并执行克隆作业.
Then you can use the alternative credential to authenticate to VSTS. Following code shows how to authenticate to VSTS and perform a clone job.
using LibGit2Sharp;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string surl = "https://youraccount.visualstudio.com/DefaultCollection/_git/Remoterepo";
string lpath = "C:\\LocalPath";
CloneOptions co = new CloneOptions();
string un = "alternativeusername";
string pwd = "alternativepassword";
Credentials ca = new UsernamePasswordCredentials() {Username = un, Password = pwd };
co.CredentialsProvider = (_url, _user, _cred) => ca ;
Repository.Clone(surl,lpath,co);
}
}
}
这篇关于您如何使用LibGit2Sharp向VSTS进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!