Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VSTS/TFS进行身份验证_Tfs_Azure Devops_Azure Devops Rest Api - Fatal编程技术网

使用VSTS/TFS进行身份验证

使用VSTS/TFS进行身份验证,tfs,azure-devops,azure-devops-rest-api,Tfs,Azure Devops,Azure Devops Rest Api,我正在升级一个与TFS/VST对话的C#应用程序,以使用最新的TeamFoundation sdk 如果您使用Visual Studio连接到TFS,我希望以与之相同的方式连接并让应用程序提示输入凭据 我从nuget.org下载了最新的稳定VSTS Api,它是: microsoft.teamfoundationserver.extendedclient.15.112.1.nupkg 我还引用了它在我的VS2017安装中使用的程序集,如下所示: C:\ProgramFiles(x86)\Micr

我正在升级一个与TFS/VST对话的C#应用程序,以使用最新的TeamFoundation sdk

如果您使用Visual Studio连接到TFS,我希望以与之相同的方式连接并让应用程序提示输入凭据

我从nuget.org下载了最新的稳定VSTS Api,它是:

microsoft.teamfoundationserver.extendedclient.15.112.1.nupkg

我还引用了它在我的VS2017安装中使用的程序集,如下所示:

C:\ProgramFiles(x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer

我尝试了许多组合,但无法使其提示。我当前的代码如下所示:

    static void Main(string[] args)
    {
        try
        {
            var netCred = new NetworkCredential();
            var basicCred = new VssBasicCredential(netCred);
            var vssCred = new VssCredentials(basicCred);
            vssCred.PromptType = CredentialPromptType.PromptIfNeeded;
            var server = new TfsTeamProjectCollection(new Uri(serverName), vssCred);
            server.Authenticate();
        }
        catch( Exception ex )
        {
            System.Console.WriteLine(ex.ToString());
        }

        System.Console.ReadKey();
    }
它不会提示,而是输出此异常:

Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: TF30063:您无权访问 https://.visualstudio.com/. ---> System.Net.WebException:异常 远程服务器返回错误:(401)未经授权。在 System.Net.HttpWebRequest.GetResponse()位于 Microsoft.TeamFoundation.Client.Channel.TfsHttpWebRequest.SendRequestAndGetResponse(HttpWebRequest webRequest、WebException和WebException)--内部异常结束 堆栈跟踪--at Microsoft.TeamFoundation.Client.Channel.TfsHttpWebRequest.SendRequest() 在 Microsoft.TeamFoundation.Client.Channel.TfsHttpRequestChannel.Request(TfsMessage 消息,TimeSpan超时)位于 Microsoft.TeamFoundation.Client.Channel.TfsHttpClientBase.Invoke(TfsClientOperation 操作、对象[]参数、时间跨度超时、对象[]和输出)
在 Microsoft.TeamFoundation.Framework.Client.LocationWebService.Connect(Int32 connectOptions、Int32 lastChangeId、Int32功能)位于 Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Connect(连接选项 连接选项)位于 Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Authenticate() 位于Microsoft.TeamFoundation.Client.TfsConnection.Authenticate()的 中的VstsAuthTest.Program.Main(字符串[]args) S:\VstsAuthTest\Program.cs:第26行

如何让它提示并缓存凭据

我使用的TeamFoundation sdk DLL的旧版本似乎可以正常工作。我之所以升级,是因为C#应用程序安装在只有VS2017而没有VS2015的机器上时,似乎拒绝连接到TFS。我希望升级到最新的SDK DLL可能有助于解决连接问题


我已经看到了这一点,但它似乎已经过时,并且使用了现在已经不推荐使用的类。这也是关于在没有提示的情况下进行连接,但是评论中包含了一些关于如何获得提示的讨论

我还看到了这些最近出现的示例,但它们也使用了不推荐的API

只需与VssClientCredentials一起使用即可

简单代码:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TFSAPIConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var u = new Uri("https://XXX.visualstudio.com");
            TfsTeamProjectCollection collection = new TfsTeamProjectCollection(u, new VssClientCredentials());

            collection.Authenticate();
            Console.WriteLine(collection.Name);
            Console.Read();

        }
    }
}

您应该切换到引用VSTS/TFS API SDK的NuGet包,并将dll与您的产品打包。不再支持依赖Visual Studio安装。这对我来说似乎还可以,但我从一些使用此代码的工具的人那里收到报告,其中包含日志,他们仍然会遇到相同的问题。Authenticate()抛出我在问题中提到的异常,并且不会向它们显示凭证提示。这些用户可以登录到VSTS web门户,并且可以与Visual Studio良好连接。您的意思是,即使在连接Visual Studio之后,他们仍然存在问题吗?尝试清除凭证管理器中的相关凭证,也清除TFS \VSTS的缓存(路径喜欢C:\用户\xxx \AppDATa\Studio \微软\ Team Foundation)是的,这就是我的意思,并且清除凭证管理器和Team Foundation Cache没有任何区别。我在未安装VS以及安装VS2015和VS2017的系统上成功地测试了该工具(使用上述代码)。显然,他们刚刚安装了VS2015。这是我目前唯一能想到的区别。如果你在代码中指定用户的帐户,它能连接到TFS/VSTS吗?我有一点感觉,这可能是因为架构。。。如果应用程序以32位运行,那么它似乎更可靠。