【问题标题】:How to authenticate to Visual Studio Team Services with the new basic authentication from a .Net Windows Service?如何使用来自 .Net Windows 服务的新基本身份验证对 Visual Studio Team Services 进行身份验证?
【发布时间】:2012-10-26 02:17:45
【问题描述】:

我正在尝试从编写在 .NET 上的 Windows 服务访问 https://visualstudio.com(以前称为 https://tfs.visualstudio.comhttp://www.tfspreview.com)。

我想使用新的基本身份验证,但找不到方法。

我找到了很多指向博客文章 Team Foundation Service updates - Aug 27 的链接,但它使用的是用于 TFS 的 Team Explorer Everywhere Java 客户端。

是否有新版本的 TFS .NET 对象模型支持基本身份验证?

顺便说一下,我已经连续使用服务帐户登录了。 This answer 非常有用。

【问题讨论】:

    标签: c# .net tfs azure-devops basic-authentication


    【解决方案1】:

    首先,您的机器上至少需要安装Visual Studio 2012 Update 1。它包括一个更新的Microsoft.TeamFoundation.Client.dll 程序集,带有BasicAuthCredential 类。

    这是执行此操作的代码,来自Buck's blog post How to connect to Team Foundation Service

    using System;
    using System.Net;
    using Microsoft.TeamFoundation.Client;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                NetworkCredential netCred = new NetworkCredential(
                    "yourbasicauthusername@live.com",
                    "yourbasicauthpassword");
                BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
                TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
                tfsCred.AllowInteractive = false;
    
                TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(
                    new Uri("https://YourAccountName.visualstudio.com/DefaultCollection"),
                    tfsCred);
    
                tpc.Authenticate();
    
                Console.WriteLine(tpc.InstanceId);
            }
        }
    }
    

    【讨论】:

    • 请注意,这需要https: 连接。
    • 我无法使其正常工作 - 显然,当您使用 Microsoft 帐户凭据时,提供的值将无法按预期工作。要针对 VSO 2015 进行身份验证,我必须启用“备用凭据”,以便我可以将用户名更改为非电子邮件地址格式。之后这段代码运行良好。
    • 同意沃尔基里斯。这不适用于 VS2015 现在需要先设置备用凭据,然后才能实际运行它。
    • 这是一个旧线程,但如果您使用 SSL,请尝试使用 SimpleWebTokenCredential 而不是 NetworkCredential 和 BasicAuthCredential。
    【解决方案2】:

    对身份验证进行了一些更新。对于 .NET 应用程序,我们现在建议使用 VSTS client libraries。另一种选择是使用 Azure Active Directory 库 (ADAL)。如需更多信息和示例,请查看VSTS's authentication documentation

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 2013-05-30
      • 2017-09-18
      • 2015-08-12
      • 2021-07-26
      • 2016-07-01
      相关资源
      最近更新 更多