以编程方式在TFS项目中查找用户

时间:2017-12-05 10:21:26

标签: c# tfs tfs2015

我们在一个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();
    }

2 个答案:

答案 0 :(得分:0)

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

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

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

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

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

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

      <强>更新

      enter image description here

答案 1 :(得分:0)

如果要查看用户的群组,可以直接调用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();

        }
    }
}

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