获取ADO/TFS项目所有用户:相关API及客户端库问题咨询
获取ADO/TFS项目所有相关用户的可行方案
1. ADO .NET客户端库实现(支持ADO Server/TFS 2013+)
直接使用官方维护的Microsoft.TeamFoundationServer.Client NuGet包,通过IdentityManagementHttpClient获取项目关联用户,这是替代已弃用GraphHttpClient的官方方案:
示例代码:
using Microsoft.VisualStudio.Services.Common; using Microsoft.VisualStudio.Services.Identity; using Microsoft.VisualStudio.Services.WebApi; // 初始化连接,TFS地址格式类似 http://tfs-server:8080/tfs/DefaultCollection var connection = new VssConnection(new Uri("你的ADO/TFS服务地址"), new VssBasicCredential("", "个人访问令牌")); var identityClient = connection.GetClient<IdentityManagementHttpClient>(); // 项目安全命名空间固定ID,无需修改 var projectSecurityNamespaceId = new Guid("58450966-6884-42a7-80d8-21444e9a3210"); // 目标项目的ID,可从项目设置页面复制 var projectId = new Guid("你的项目ID"); // 获取项目直接关联的用户/组 var identities = await identityClient.ListIdentityReferencesAsync( projectSecurityNamespaceId, $"vstfs:///Classification/TeamProject/{projectId}", queryMembership: QueryMembership.Direct ); // 遍历处理结果 foreach (var identity in identities) { Console.WriteLine($"名称:{identity.DisplayName},标识:{identity.UniqueName}"); }
2. REST API方式(跨平台通用)
如果不需要.NET库,直接调用ADO REST API即可,兼容所有支持REST的ADO/TFS版本:
- 请求方法:
GET - 请求地址:
https://{实例地址}/{集合名}/{项目名}/_apis/identities?queryMembership=direct&api-version=7.1-preview.1 - 身份验证:将个人访问令牌(PAT)进行Base64编码(格式为
空用户名:PAT),放在Authorization头中,值为Basic {编码后字符串}
3. 关于Microsoft Graph的说明
Microsoft Graph聚焦于Azure AD全局身份管理,并非ADO项目用户的最优获取途径。只有当项目完全集成Azure AD时,才能通过Graph查询项目对应的AD组成员,但这种方式不支持未集成Azure AD的传统TFS环境,因此更推荐使用ADO原生API。
关键注意点
- TFS 2013及以上版本都支持上述两种方案
- 个人访问令牌需要配置项目集合读取或项目读取权限
- 已弃用的
GraphHttpClient确实不再维护,官方明确推荐IdentityManagementHttpClient作为替代
内容的提问来源于stack exchange,提问作者Aman Varshney
相关产品推荐
相关产品推荐

