【问题标题】:Finding user in TFS Project programmatically以编程方式在 TFS 项目中查找用户
【发布时间】:2018-05-18 23:18:17
【问题描述】:

我们在一台 TFS 服务器下有多个集合,每个集合都有多个项目。我希望能够检查输入的用户名是否是 TFS 服务器上任何项目级别组的一部分。 到目前为止,我能够连接到 TFS,并获取集合下的所有项目名称。我需要帮助来查找组名,然后查询这些组以检查用户是否属于该组。

这是我尝试过的代码 -

static void Main(string[] args)
    {
        NetworkCredential netCred = new NetworkCredential(@"username", @"pwd");
        TfsConfigurationServer configServ = new TfsConfigurationServer(new Uri("https://my-tfs.schwab.com/tfs"), netCred);

        var tfsAllCols = new List<KeyValuePair<Guid, string>>();
        try
        {
            configServ.Authenticate();
            Console.WriteLine("Autheticated in server with ad creds...");

            ReadOnlyCollection<CatalogNode> colNodes = configServ.CatalogNode.QueryChildren(
                new[] { CatalogResourceTypes.ProjectCollection },
                false,
                CatalogQueryOptions.None);
            foreach (CatalogNode node in colNodes)
            {
                var colId = new Guid(node.Resource.Properties["InstanceId"]);
                TfsTeamProjectCollection teamProjectCollection =
                    configServ.GetTeamProjectCollection(colId);

                tfsAllCols.Add(new KeyValuePair<Guid, string>(colId, teamProjectCollection.Name));

            }

    //hardcoding the colname for testing
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("https://ruby-tfs.schwab.com/tfs/colname/"), netCred);

            tpc.EnsureAuthenticated();

            // Get the catalog of team project collections
            ReadOnlyCollection<CatalogNode> projNodes = tpc.CatalogNode.QueryChildren(
            new[] { CatalogResourceTypes.TeamProject },
            false, CatalogQueryOptions.None);

        }
        catch (Exception ex)
        {

            throw ex;
        }

        Console.ReadLine();
    }

【问题讨论】:

标签: c# tfs tfs-2015


【解决方案1】:

您可以使用TFSSecurity command-line tool 来检索团队项目中的组和成员,而不是硬编码:

  1. 使用 /g 列出团队项目、团队项目集合或 Team Foundation Server 中的组:

tfssecurity /g [scope] [/collection:CollectionURL] [/server:ServerURL]

  1. 使用 /imx 显示有关组成指定组扩展成员身份的身份信息:
> tfssecurity /imx Identity [/collection:CollectionURL][/server:ServerURL]

如果你坚持硬编码,你可以按照下面的博客查询 TFS 的组和成员:

https://blogs.msdn.microsoft.com/vasu_sankaran/2010/10/11/querying-tfs-for-groups-and-memberships/

更新:

【讨论】:

  • 我只需要对其进行硬编码。感谢您的参考网址,我将通过这个。
  • 您也可以考虑在您的代码中调用 TFSSecurity 命令行工具。
  • 顺便说一句,我尝试了 ref url 中的代码。它仅显示组(ROLE.* 组),但不显示这些组的成员名称
  • @arpymastro 你能得到组的成员吗?
【解决方案2】:

如果要查看用户所属的组,可以直接调用ims.ReadIdentity()方法。以下是供您参考的简单代码:

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Framework.Client;
using Microsoft.TeamFoundation.Framework.Common;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.Common;

namespace ConsoleApplication3

{

    class Program

    {
        static void Main(string[] args)

        {

            TfsConfigurationServer tcs = new TfsConfigurationServer(new Uri("http://xxxx:8080/tfs/"));

            IIdentityManagementService ims = tcs.GetService<IIdentityManagementService>();

            TeamFoundationIdentity tfi = ims.ReadIdentity(IdentitySearchFactor.AccountName, "domain\\username", MembershipQuery.Direct, ReadIdentityOptions.None);
            Console.WriteLine("Listing Groups for:" + tfi.DisplayName);
            Console.WriteLine("Total " + tfi.MemberOf.Length + " groups.");
            IdentityDescriptor[] group = tfi.MemberOf;
            foreach (IdentityDescriptor id in group)
            {
                TeamFoundationIdentity detail = ims.ReadIdentity(IdentitySearchFactor.Identifier, id.Identifier, MembershipQuery.None, ReadIdentityOptions.None);
                Console.WriteLine(detail.DisplayName);
            }
            Console.ReadLine();

        }
    }
}

如果这不符合您的要求,您也可以使用此方法获取每个项目组的成员。

【讨论】:

    猜你喜欢
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2013-11-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 2019-01-07
    相关资源
    最近更新 更多