You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在C#中使用REST API检索Azure DevOps Server 2022中区域路径的关联组 | Azure DevOps Server 2022

如何在C#中使用REST API检索Azure DevOps Server 2022中区域路径的关联组 | Azure DevOps Server 2022

嗨,恭喜你已经成功获取到项目下的区域路径啦!要拿到这些区域路径关联的Azure DevOps组,我们得借助Azure DevOps的权限管理API来实现——毕竟区域路径的权限(包括关联组)是通过安全命名空间来管控的。下面我一步步给你讲具体怎么操作:

核心思路说明

区域路径的权限数据存储在名为Classifications的安全命名空间下,对应的固定GUID是58450C49-B02D-465A-82FD-8DAAAF8B315A。我们需要先拿到目标区域路径的ID,构造对应的安全令牌,再调用权限API获取关联的权限条目,最后从中筛选出组的信息。

具体步骤与C#代码示例

  1. 获取目标区域路径的ID
    你已经实现了获取区域路径的功能,从返回的结果里找到目标区域路径的id字段值就行,比如123(替换成你的实际ID)。

  2. 构造安全令牌并编码
    每个区域路径对应一个安全令牌,格式是vstfs:///Classification/Node/{AreaNodeId},因为令牌包含特殊字符,需要做URL编码。

  3. 调用权限API获取关联条目
    用HttpClient发送请求,这里支持PAT认证或者Windows域认证(适合企业内网的Azure DevOps Server)。

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

class AzDOAreaGroupsRetriever
{
    static async Task Main(string[] args)
    {
        // 替换成你的实际配置
        string tfsServerUrl = "http://your-devops-server-url:8080/tfs";
        string collectionName = "YourCollection";
        string projectName = "YourTargetProject";
        string personalAccessToken = "your-pat-token-here"; // Windows认证可注释掉这行用默认凭据
        int targetAreaNodeId = 123; // 替换成你拿到的区域路径ID

        // 构造区域路径的安全令牌并编码
        string areaSecurityToken = $"vstfs:///Classification/Node/{targetAreaNodeId}";
        string encodedToken = Uri.EscapeDataString(areaSecurityToken);

        // Classifications安全命名空间的固定GUID
        const string classificationNamespaceId = "58450C49-B02D-465A-82FD-8DAAAF8B315A";

        // 构造API请求地址
        string apiEndpoint = $"{tfsServerUrl}/{collectionName}/_apis/securitynamespaces/{classificationNamespaceId}/tokens/{encodedToken}?api-version=7.1-preview.1";

        using (HttpClient client = new HttpClient())
        {
            // PAT认证方式
            var base64Auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($":{personalAccessToken}"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Auth);

            // Windows域认证方式(替换上面的PAT认证代码)
            // var handler = new HttpClientHandler { UseDefaultCredentials = true };
            // using (HttpClient client = new HttpClient(handler))

            // 发送请求并获取响应
            HttpResponseMessage response = await client.GetAsync(apiEndpoint);
            response.EnsureSuccessStatusCode(); // 确保请求成功

            string responseContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine("权限条目响应结果:");
            Console.WriteLine(responseContent);
        }
    }
}
  1. 筛选并处理组信息
    API返回的JSON里,accessControlEntries数组包含了所有关联的权限主体(用户和组):
    • 组的descriptor前缀是vssgp.,用户的是vssuser.,可以通过这个前缀筛选出组的条目;
    • displayName字段直接显示组的名称,如果需要更详细的组信息(比如组成员、描述等),可以调用/_apis/identities API,传入组的descriptor来获取,示例地址:
      {tfsServerUrl}/{collectionName}/_apis/identities?descriptors={encodedDescriptor}&api-version=7.1-preview.1
      
      这里的encodedDescriptor需要对组的descriptor做URL编码。

额外注意事项

  • API版本:我用的7.1-preview.1兼容Azure DevOps Server 2022,你也可以换成稳定版7.0
  • 权限继承:如果区域路径继承了父节点的权限,API会返回继承的条目,你可以通过includeExtendedInfo=false参数或者结果里的inherited字段来区分直接分配和继承的权限;
  • 权限范围:确保你的PAT或者账号有读取项目权限的权限(比如“项目集合管理员”或“项目管理员”权限),否则会返回403错误。

备注:内容来源于stack exchange,提问作者Karthik Krishna Baiju

火山引擎 最新活动