2017-07-18 336 views
0

我正在升级与TFS/VSTS通信以使用最新的TeamFoundation sdk的C#应用​​程序。使用VSTS/TFS进行身份验证

我想以Visual Studio的相同方式连接并获得应用程序提示输入凭据,如果您使用它连接到TFS。

我已经下载从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: You are not authorized to access https://.visualstudio.com/. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequestAndGetResponse(HttpWebRequest webRequest, WebException& webException) --- End of inner exception stack trace --- at Microsoft.TeamFoundation.Client.Channels.TfsHttpWebRequest.SendRequest() at Microsoft.TeamFoundation.Client.Channels.TfsHttpRequestChannel.Request(TfsMessage message, TimeSpan timeout) at Microsoft.TeamFoundation.Client.Channels.TfsHttpClientBase.Invoke(TfsClientOperation operation, Object[] parameters, TimeSpan timeout, Object[]& outputs)
at 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() at Microsoft.TeamFoundation.Client.TfsConnection.Authenticate() at VstsAuthTest.Program.Main(String[] args) in S:\VstsAuthTest\Program.cs:line 26

我如何得到它的提示和缓存凭据?

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


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

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

我也看到了这些样品中出现近,但也使用淘汰的API。

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

+0

您应该切换到引用NuGet包的VSTS/TFS API SDK,并与您的产品包装DLL的。不再支持依赖于Visual Studio安装。 – jessehouwing

回答

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(); 

     } 
    } 
} 
+0

这似乎对我来说确实很好,但是我收到了几个使用此代码的工具的日志报告,他们仍然遇到同样的问题。 Authenticate()抛出我在问题中提到的异常,并且它们没有显示提示输入凭据。这些用户可以登录到VSTS门户网站,并且可以很好地与Visual Studio连接。 –

+0

即使与Visual Studio连接后,你的意思是他们有问题吗?尝试清除凭证管理器中的相关凭证,并清除TFS \ VSTS的缓存(路径类似C:\ Users \ XXX \ AppData \ Local \ Microsoft \ Team Foundation) –

+0

是的,这就是我的意思,并清除凭证管理器和Team Foundation缓存没有任何区别。我在没有安装VS的系统上,以及安装了VS2015和VS2017的系统上成功测试了该工具(使用上面的代码)。显然,他们只是安装了VS2015。这是我现在能想到的唯一区别。 –