You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从TFS用户配置文件信息中获取Locale?

获取TFS用户配置文件中的Locale信息

我之前也碰到过类似的需求,下面分享两种可行的方法来获取TFS用户配置文件里的Locale信息:

方法一:使用TFS REST API

这是最直接的方式,不需要额外依赖库,通过HTTP请求就能拿到数据:

  1. 先获取用户的Descriptor:如果不知道用户的userDescriptor,可以先通过用户名或用户ID调用身份API获取:

    GET https://{tfs-instance-url}/{collection}/_apis/identities?searchFilter=General&value={username-or-id}&api-version=6.0
    

    响应里的descriptor字段就是我们需要的userDescriptor。

  2. 调用用户配置文件API获取Locale:

    GET https://{tfs-instance-url}/{collection}/_apis/profile/profiles/{userDescriptor}?api-version=6.0
    

    在返回的JSON响应中,locale字段就是用户的区域设置信息(比如en-US、zh-CN)。

方法二:使用.NET客户端库

如果你的项目是基于.NET的,可以用官方的客户端库来简化操作:

  1. 安装NuGet包:
    Install-Package Microsoft.TeamFoundationServer.Client
    
  2. 编写代码获取Locale:
    using Microsoft.VisualStudio.Services.Common;
    using Microsoft.VisualStudio.Services.Profile;
    using Microsoft.TeamFoundation.Client;
    
    var tfsUrl = new Uri("https://{tfs-instance-url}/{collection}");
    var credentials = new VssBasicCredential("username", "password-or-pat"); // 也可以用其他认证方式
    
    using (var tfsCollection = new TfsTeamProjectCollection(tfsUrl, credentials))
    {
        tfsCollection.EnsureAuthenticated();
        var profileClient = tfsCollection.GetClient<ProfileHttpClient>();
        
        // 可通过用户ID或Descriptor获取配置文件
        var profile = await profileClient.GetProfileAsync(descriptor: "{userDescriptor}");
        var userLocale = profile.Locale;
        
        Console.WriteLine($"用户Locale: {userLocale}");
    }
    

注意事项

  • 确保你的账号拥有读取用户配置文件的权限,否则会返回权限错误。
  • 不同版本的TFS可能需要调整API版本号,比如TFS 2018对应api-version=4.1,Azure DevOps Server 2020对应api-version=6.0,可以根据你的环境调整。

内容的提问来源于stack exchange,提问作者Manikanta Cv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:53:48