2011-11-05 64 views
3

我想以編程方式將用戶添加到團隊項目。我發現解決方案是這樣的:如何以編程方式向TFS添加用戶

IGroupSecurityService gss = (IGroupSecurityService)objTFS.GetService(typeof(IGroupSecurityService)); 
Identity identity = gss.ReadIdentity(SearchFactor.AccountName, "Group name", QueryMembership.None); 
gss.AddMemberToApplicationGroup(groupProject.Sid, member.Sid); 

但是,這隻適用於TFS已知的組/用戶。

我想在Windows帳戶添加到TFS

例如:

Windows帳戶名:TestTFS

密碼:123456

然後TestTFS添加到TFS編程。

我知道有一個名爲TeamFoundation Administration Tool的工具可以做到這一點,但我不想使用它。

+0

檢查了這一點http://stackoverflow.com/questions/7961727/how-to-grant-read-only-access-to- all-tfs-team-projects-to-a-of-users/7971731#7971731 –

回答

1

爲了通過TFS API執行此操作,您需要訪問2級信息。

  1. 您要添加的用戶的SID,

  2. 您在您的文章顯示您想將用戶添加到 =>代碼組的SID將讓你的ID您想要添加用戶的組。

我發現示例代碼2個鏈接我覺得可以對你有所幫助,

+0

但我仍然有一個問題,我如何才能得到我想添加的用戶的sid ....只需使用Identity userIdentity = tfsSecurityService.ReadIdentity(SearchFactor.AccountName,userName,QueryMembership.Direct);但我認爲這隻適用於TFS已知的用戶......我如何才能獲得TFS未知的用戶的sid ......是否有某種我以錯誤的方式理解的內容? – Leslie

+0

Koders網站已死亡*** – Kiquenet

2

我有同樣的問題,最後下面的代碼工作

  NTAccount f = new NTAccount(userName); 
      SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier)); 
      string userSid = s.ToString(); 

名字空間:using System.Security.Principal;

6

在TFS2012中,IGroupSecurityService被標記爲已過時,用IIdentityManagementService替換。

您可以使用IIdentityManagementService.ReadIdentity()IIdentityManagementService.AddMemberToApplicationGroup()將Windows用戶添加到TFS組,即使這些Windows用戶還不知道TFS。

這是通過指定ReadIdentityOptions.IncludeReadFromSource選項來完成的。

下面是添加一個Windows用戶VSALM\BarryFabrikam Fiber Web Team(TFS集團),在FabrikamFiber團隊項目中,http://vsalm:8080/tfs/FabrikamFiberCollection服務器/集合中的一個例子。

您需要將引用添加到:Microsoft.TeamFoundation.ClientMicrosoft.TeamFoundation.Common

using Microsoft.TeamFoundation.Client; 
using Microsoft.TeamFoundation.Framework.Client; 
using Microsoft.TeamFoundation.Framework.Common; 
using System; 

namespace ConsoleApplication1 
{ 
    class Program 
     { 
     static void Main(string[] args) 
     { 
      var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://vsalm:8080/tfs/FabrikamFiberCollection")); 

      var ims = tpc.GetService<IIdentityManagementService>(); 

      var tfsGroupIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName, 
                "[FabrikamFiber]\\Fabrikam Fiber Web Team", 
                MembershipQuery.None, 
                ReadIdentityOptions.IncludeReadFromSource);    

      var userIdentity = ims.ReadIdentity(IdentitySearchFactor.AccountName, 
                "VSALM\\Barry", 
                MembershipQuery.None, 
                ReadIdentityOptions.IncludeReadFromSource); 

      ims.AddMemberToApplicationGroup(tfsGroupIdentity.Descriptor, userIdentity.Descriptor); 
     } 
    } 
} 
相關問題