使用VSTS / TFS进行身份验证

时间:2017-07-18 11:00:13

标签: tfs azure-devops azure-devops-rest-api

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

如果您使用连接到TFS,我想以Visual Studio所做的相同方式连接并获取凭据的应用提示。

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

microsoft.teamfoundationserver.extendedclient.15.112.1.nupkg

我还引用了我在VS2017安装中使用的程序集,这里:

C:\Program Files (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:The   远程服务器返回错误:(401)未经授权。在   System.Net.HttpWebRequest.GetResponse()at   Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequestAndGetResponse(HttpWebRequest的   webRequest,WebException& webException)---内部异常结束   堆栈跟踪--- at   Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequest()   在   Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage   消息,TimeSpan超时)at   Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation   operation,Object []参数,TimeSpan超时,对象[]&输出)
  在   Microsoft.TeamFoundation.Framework.Client.LocationWebService.Connect(的Int32   connectOptions,Int32 lastChangeId,Int32 features)at   Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Connect(ConnectOptions   connectOptions)at   Microsoft.TeamFoundation.Framework.Client.FrameworkServerDataProvider.Authenticate()   在Microsoft.TeamFoundation.Client.TfsConnection.Authenticate()at   VstsAuthTest.Program.Main(String [] args)in   S:\ VstsAuthTest \ Program.cs:第26行

如何让它提示并缓存凭据?

我使用的旧版TeamFoundation sdk dlls似乎工作正常。我升级的原因是因为C#app在安装在只有VS2017而不是VS2015的机器上时似乎拒绝连接到TFS。我希望升级到最新的SDK dll可能有助于解决连接问题。

我已经看过了,但似乎已经过时了并且使用了现已弃用的类。它也是关于没有提示的连接,但评论包括一些关于如何获得提示的讨论。

https://blogs.msdn.microsoft.com/buckh/2013/01/07/how-to-connect-to-tf-service-without-a-prompt-for-liveid-credentials/

我也看到这些样本最近出现,但也使用了弃用的apis。

https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/samples

1 个答案:

答案 0 :(得分:1)

只需将Microsoft Team Foundation Server Extended Client package与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();

        }
    }
}