我正在升级与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可能有助于解决连接问题。
我已经看到了这一点,但它似乎已经过时,并使用了现在过时的类。这也是关于没有提示的连接,但是评论包括一些关于如何获得提示的讨论。
我也看到了这些样品中出现近,但也使用淘汰的API。
https://www.visualstudio.com/en-us/docs/integrate/get-started/client-libraries/samples
您应该切换到引用NuGet包的VSTS/TFS API SDK,并与您的产品包装DLL的。不再支持依赖于Visual Studio安装。 – jessehouwing