如何确定当前用户的存储键或描述符

问题描述 投票:0回答:2

许多 Azure DevOps API 在引用用户时都将存储密钥作为输入。该存储密钥是您所在组织中用户的唯一标识符。我似乎无法弄清楚如何确定当前登录用户的存储密钥。

我知道我可以调用 Get Profile API 并指定

Me
作为 id,但这通常会返回有关当前用户的信息 - 与特定组织无关。

List Users api 可用于获取组织中所有用户的列表,但我不想通过搜索结果来获取登录用户。 Get User api 需要一个用户描述符,并且似乎不允许我指定
Me
来获取当前用户。

那么我该如何获取当前用户的存储密钥(甚至描述符)用户?

azure-devops azure-devops-rest-api
2个回答
0
投票

通过

Get Profile API
得到的descriptor,它只能代表用户自己。换句话说,它是访问服务器的身份
dev.azure.com
(不是组织,只是由服务器识别)。所以这里的
descriptor
不能用于Org,因为无法在其中识别。

此外,对于Get User apiList User api,它们都是组织级别的,这意味着它的查询结果将是整个组织的查询结果。而且我们没有为 Azure Devops rest api 提供

filter
功能。

所以,如果想

filter
通过在 API url 中指定一个用户,不幸的是,抱歉,当您使用 Azure Devops rest api 时,这不支持。您必须遵循文档中显示的限制 URL 格式。


作为解决方法,您可以使用 powershell 脚本来实现过滤器。

您可以在 Azure devops 管道中执行脚本:

$url = 'https://vssps.dev.azure.com/{org}/_apis/graph/users?api-version=5.0-preview.1';
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method Get
$results = $response.value | Where {$_.displayname -eq "{your account name}"} #|
Write-Host "results = $($results | ConvertTo-Json -Depth 100)"
Write-Host $results.desciptor

或者在Powershell ISE中本地运行,比上一个更方便:

然后您可以将

desciptor
存储到一个参数中并在Storage Keys - Get API中使用它。


0
投票

connectionData
API 端点可能对您有所帮助。似乎微软忘记了正确记录它,但返回的对象是通过 azure-devops-extension-api JavaScript 库记录在https://learn.microsoft.com/en-us/javascript/api/azure- devops-extension-api/connectiondata.

这里是一个使用 Azure DevOps 管道的例子

$contentType = "application/json";
$headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
$uri = "$(System.CollectionUri)/_apis/connectionData?api-version=7.0-preview.1";
$connectionData = Invoke-RestMethod -uri $uri -method GET -Headers $headers -ContentType $contentType
# The VSID a.k.a. Storage Key of the current user, E.g. "e2f6c43a-c231-4f82-841a-bfc82920ffaa"
$authorizedUserVSId = $connectionData.authorizedUser.id
# The "old" Identity Descriptor, e.g. "Microsoft.IdentityModel.Claims.ClaimsIdentity;9e57a836-f042-42ae-83bd-49f609cbd058\[email protected]"
$authorizedUserIdentityDescriptor = $connectionData.authorizedUser.descriptor
# The "new" Graph User Descriptor a.k.a Subject Descriptor. E.g. "aad.ZTJmNmM0M2EtYzIzMS00ZjgyLTg0MWEtYmZjODI5MjBmZmFh"
$authorizedUserSubjectDescriptor = $connectionData.authorizedUser.subjectDescriptor
© www.soinside.com 2019 - 2024. All rights reserved.