嗯,这是可能的,尽管不推荐;我也需要它,因为内部 IT 部门不会使用 HTTPS 安装 TFS(可悲的故事)。此外,对于测试场景,它可以派上用场。
与往常一样,YMMV 我不对你不应该使用它时发生的事情负责;-) 你已被警告过。
你可以不使用 .NET 客户端 API,而直接使用 HttpClient 并手动将 PAT 放入 URL 以访问 REST API,例如:
http://<WHATEVER>:<BASE64PAT>@<instance>/_apis/...
(因此tfx-cli 可以很好地与 PAT 和非 HTTPS TFS 实例配合使用,很可能是因为它只是在内部执行此操作,当然不使用 .NET 客户端 API - 这是一个 node.js 的东西。)
如果您想继续使用 .NET 客户端 API,您可以像这样创建自己的凭据类:
using System;
using System.Linq;
using System.Net;
using Microsoft.VisualStudio.Services.Common;
namespace Utilities
{
/// <summary>
/// Same as VssBasicCredential, but doesn't throw when URL is a non SSL, i.e. http, URL.
/// </summary>
/// <inheritdoc cref="FederatedCredential"/>
internal sealed class PatCredentials : FederatedCredential
{
public PatCredentials()
: this((VssBasicToken)null)
{
}
public PatCredentials(string userName, string password)
: this(new VssBasicToken(new NetworkCredential(userName, password)))
{
}
public PatCredentials(ICredentials initialToken)
: this(new VssBasicToken(initialToken))
{
}
public PatCredentials(VssBasicToken initialToken)
: base(initialToken)
{
}
public override VssCredentialsType CredentialType => VssCredentialsType.Basic;
public override bool IsAuthenticationChallenge(IHttpResponse webResponse)
{
if (webResponse == null ||
webResponse.StatusCode != HttpStatusCode.Found &&
webResponse.StatusCode != HttpStatusCode.Found &&
webResponse.StatusCode != HttpStatusCode.Unauthorized)
{
return false;
}
return webResponse.Headers.GetValues("WWW-Authenticate").Any(x => x.StartsWith("Basic", StringComparison.OrdinalIgnoreCase));
}
protected override IssuedTokenProvider OnCreateTokenProvider(Uri serverUrl, IHttpResponse response)
{
return new BasicAuthTokenProvider(this, serverUrl);
}
private sealed class BasicAuthTokenProvider : IssuedTokenProvider
{
public BasicAuthTokenProvider(IssuedTokenCredential credential, Uri serverUrl)
: base(credential, serverUrl, serverUrl)
{
}
protected override string AuthenticationScheme => "Basic";
public override bool GetTokenIsInteractive => this.CurrentToken == null;
}
}
}
然后使用这个类创建你的 VssCredentials:
var credentials = new PatCredentials("", personalAccessToken);
var connection = new VssConnection(serverUrl, credentials);
(无耻插件我用在我的TfsInfoService)。