在我们的内部网络中使用 TFS,想要通过访问 token 以编程方式 checkin 更改,但得到这个: InvalidOperationException 基本身份验证需要到服务器的安全连接。有没有办法关闭需要安全连接?

var basicCreds = new VssBasicCredential(string.Empty, BuildUnitTestConstants.AccessToken);
var connection = new VssConnection(new Uri(BuildUnitTestConstants.ProjectUri), basicCreds);
var sourceControlServer = connection.GetClient<TfvcHttpClient>();

最佳答案

嗯,这是可能的,尽管不推荐;我也需要它,因为内部 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 中使用它)。

关于tfs - 带有访问 token 的非 https VssConnection - 禁用所需的安全连接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46249713/

10-14 13:11
查看更多