Azure active directory 如何在azure active directory中从userprincipalname检索用户ID?
我的客户端在本地Active Directory AD和Azure Active Directory AAD之间设置了同步 我能够使用MicrosoftGraph从AAD检索用户信息,但我特别需要获取AD用户ID,即{domain}/{UserID} 我试着打电话https://graph.microsoft.com/v1.0/users/firstname.lastname@domain.com/$select=userId,但它不起作用 我的问题是,可能吗?在这种情况下,实际的属性名是什么?我一直在四处寻找,但没有找到完整的属性列表 编辑: 在收到Marilee的一个答案后,我把我一直在使用的C代码包括在内,ish。这两个调用都可以从AAD接收用户信息,但不能接收我正在寻找的AD用户ID,即{domain}/{UserID} 第一次尝试Azure active directory 如何在azure active directory中从userprincipalname检索用户ID?,azure-active-directory,microsoft-graph-api,Azure Active Directory,Microsoft Graph Api,我的客户端在本地Active Directory AD和Azure Active Directory AAD之间设置了同步 我能够使用MicrosoftGraph从AAD检索用户信息,但我特别需要获取AD用户ID,即{domain}/{UserID} 我试着打电话https://graph.microsoft.com/v1.0/users/firstname.lastname@domain.com/$select=userId,但它不起作用 我的问题是,可能吗?在这种情况下,实际的属性名是什么?
var requestUri = GraphBaseUri + $"/v1.0/users/{upn}?$select=userId";
var response = await _httpClient.GetAsync(new AuthenticationHeaderValue("Bearer", accessToken), requestUri).ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
dynamic responseObj = JsonConvert.DeserializeObject(content) as JObject;
return responseObj.UserId; //NOT WORKING
第二次尝试
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
return Task.FromResult(0);
}));
// Retrieve a user by userPrincipalName
var user = await graphClient
.Users[upn]
.Request()
.GetAsync();
return user.ObjectId; //NOT WORKING
您所指的属性是objectID。从Graph API中,您可以使用UPN,如您所说:
GET /users/{id | userPrincipalName}
您可以通过几种不同的方式查找用户。从/users端点,您可以使用其id(分配给每个帐户的GUID)或其userPrincipalName(默认域的电子邮件别名):
//按id检索用户
var user=wait graphClient
.用户[00000000-0000-0000-0000-000000000000]
要求
.GetAsync;
//按userPrincipalName检索用户
var user=wait graphClient
.用户[user@tenant.onmicrosoft.com]
要求
.GetAsync;
如果您正在使用授权代码或隐式OAuth授权,还可以查找通过/me端点进行身份验证的用户:
var user=wait graphClient
我
要求
.GetAsync;
从Powershell,您可以查询对象ID:
$Get AzureADUser-过滤器UserPrincipalName eq'myuser@consoso.com’ObjectId
做到这一点的方法是
var requestUri = GraphBaseUri + $"/v1.0/users/{upn}?$select=onPremisesSamAccountName";
var response = await _httpClient.GetAsync(new AuthenticationHeaderValue("Bearer", accessToken), requestUri).ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var responseObj = JsonConvert.DeserializeObject<ResponseObj>(content);
return responseObj.OnPremisesSamAccountName;
请注意,选择收集onPremisesSamAccountName的位置。尽管如此,我还没有找到一个可以检索到的所有属性的完整列表。这听起来很有希望,但我可能没有收到使用C的ObjectId,这正是我当时需要的。你想使用C检索吗?这是可以做到的。这不是一个移动应用程序,而是azure函数中的代码。我所记得的是,我正在使用Microsoft.Graph 1.21.0和Microsoft.Graph.Core 1.19.0检索ObjectId作为上面的编辑。在用户对象中找不到ObjectId属性。