如何从TFS用户配置文件信息中获取Locale?
获取TFS用户配置文件中的Locale信息
我之前也碰到过类似的需求,下面分享两种可行的方法来获取TFS用户配置文件里的Locale信息:
方法一:使用TFS REST API
这是最直接的方式,不需要额外依赖库,通过HTTP请求就能拿到数据:
先获取用户的Descriptor:如果不知道用户的
userDescriptor,可以先通过用户名或用户ID调用身份API获取:GET https://{tfs-instance-url}/{collection}/_apis/identities?searchFilter=General&value={username-or-id}&api-version=6.0响应里的
descriptor字段就是我们需要的userDescriptor。调用用户配置文件API获取Locale:
GET https://{tfs-instance-url}/{collection}/_apis/profile/profiles/{userDescriptor}?api-version=6.0在返回的JSON响应中,
locale字段就是用户的区域设置信息(比如en-US、zh-CN)。
方法二:使用.NET客户端库
如果你的项目是基于.NET的,可以用官方的客户端库来简化操作:
- 安装NuGet包:
Install-Package Microsoft.TeamFoundationServer.Client - 编写代码获取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
相关产品推荐
相关产品推荐

