读取
customSecurityAttributes
属性:
-
在委派方案中,必须为管理员分配
属性分配管理员
角色,并且应用被授予
CustomSecAttributeAssignment.Read.All
权限。
-
在具有 Microsoft Graph 权限的仅限应用方案中,必须向应用授予
CustomSecAttributeAssignment.Read.All
权限。
-
当
userPrincipalName
以
$
字符开头时,GET 请求 URL 语法
/users/$x@y.com
失败,并出现
400 Bad Request
错误代码。 这是因为此请求 URL 违反了 OData URL 约定,该约定要求只有系统查询选项才能以
$
字符作为前缀。 删除
/users
后面的斜杠 (/),并将
userPrincipalName
括在圆括号和单引号中,如下所示:
/users('$x@y.com')
。 例如,
/users('$AdeleVance@contoso.com')
。
-
要使用
userPrincipalName
查询 B2B 用户,请对哈希 (#) 字符进行编码。 也就是说,将
#
符号替换为
%23
。 例如,
/users/AdeleVance_adatum.com%23EXT%23@contoso.com
。
对于登录用户:
GET /me
可选的查询参数
此方法支持 $select
OData 查询参数 来检索特定用户属性,包括默认情况下不返回的属性。
默认情况下,仅返回一组有限的属性(businessPhones、displayName、givenName、id、jobTitle、mail、mobilePhone、officeLocation、preferredLanguage、surname、userPrincipalName)。
若要返回其他属性,必须使用 OData $select
查询参数指定所需的一组 user 属性。 例如,若要返回 displayName、 givenName 和 postalCode,请将以下表达式添加到查询 $select=displayName,givenName,postalCode
。
扩展属性还支持查询参数,如下所示:
请勿提供此方法的请求正文。
如果成功,此方法在响应正文中返回 200 OK
响应代码和 user 对象。 除非使用 $select
指定特定属性,否则返回默认属性。 当成功处理请求时,此方法会返回 202 Accepted
,但服务器需要更多时间来完成相关的后台操作。
如果 ID 为的用户不存在,此方法将 404 Not Found
返回错误代码。
示例 1:标准用户请求
默认情况下,仅返回一组有限的属性(businessPhones、displayName、givenName、id、jobTitle、mail、mobilePhone、officeLocation、preferredLanguage、surname、userPrincipalName)。 此示例展示了默认请求和响应。
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users["{user-id}"].GetAsync();
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
users, err := graphClient.Users().ByUserId("user-id").Get(context.Background(), nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
User result = graphClient.users().byUserId("{user-id}").get();
const client = Client.init(options);
let user = await client.api('/users/87d349ed-44d7-43e1-9a83-5f2406dee5bd')
.get();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.users.by_user_id('user-id').get()
"jobTitle": "Retail Manager",
"mail": "AdeleV@contoso.com",
"mobilePhone": "+1 425 555 0109",
"officeLocation": "18/2111",
"preferredLanguage": "en-US",
"surname": "Vance",
"userPrincipalName": "AdeleV@contoso.com",
"id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
示例 2:登录用户请求
可以将 /users/{id | userPrincipalName}
替换为 /me
,获取登录用户的用户信息。
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Me.GetAsync();
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
//other-imports
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
me, err := graphClient.Me().Get(context.Background(), nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
User result = graphClient.me().get();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
result = await graph_client.me.get()
"jobTitle": "Retail Manager",
"mail": "AdeleV@contoso.com",
"mobilePhone": "+1 425 555 0109",
"officeLocation": "18/2111",
"preferredLanguage": "en-US",
"surname": "Vance",
"userPrincipalName": "AdeleV@contoso.com",
"id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
示例 3:使用 $select 检索用户的特定属性
若要检索特定属性,请使用 OData $select
查询参数。 例如,若要返回 displayName、givenName、postalCode 和 identities,则需要将以下内容添加到查询 $select=displayName,givenName,postalCode,identities
。
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users["{user-id}"].GetAsync((requestConfiguration) =>
requestConfiguration.QueryParameters.Select = new string []{ "displayName","givenName","postalCode","identities" };
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
//other-imports
requestParameters := &graphusers.UserItemRequestBuilderGetQueryParameters{
Select: [] string {"displayName","givenName","postalCode","identities"},
configuration := &graphusers.UserItemRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
users, err := graphClient.Users().ByUserId("user-id").Get(context.Background(), configuration)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
User result = graphClient.users().byUserId("{user-id}").get(requestConfiguration -> {
requestConfiguration.queryParameters.select = new String []{"displayName", "givenName", "postalCode", "identities"};
const client = Client.init(options);
let user = await client.api('/users/87d349ed-44d7-43e1-9a83-5f2406dee5bd')
.select('displayName,givenName,postalCode,identities')
.get();
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\UserItemRequestBuilderGetRequestConfiguration;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestConfiguration = new UserItemRequestBuilderGetRequestConfiguration();
$queryParameters = UserItemRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->select = ["displayName","givenName","postalCode","identities"];
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->users()->byUserId('user-id')->get($requestConfiguration)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.users.item.user_item_request_builder import UserItemRequestBuilder
from kiota_abstractions.base_request_configuration import RequestConfiguration
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(
select = ["displayName","givenName","postalCode","identities"],
request_configuration = RequestConfiguration(
query_parameters = query_params,
result = await graph_client.users.by_user_id('user-id').get(request_configuration = request_configuration)
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(displayName,givenName,postalCode,identities)/$entity",
"displayName": "Adele Vance",
"givenName": "Adele",
"postalCode": "98004",
"identities": [
"signInType": "userPrincipalName",
"issuer": "contoso.com",
"issuerAssignedId": "AdeleV@contoso.com"
示例 4:获取用户的架构扩展值
在此示例中,架构扩展 ID 为 ext55gb1l09_msLearnCourses
。
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users["{user-id}"].GetAsync((requestConfiguration) =>
requestConfiguration.QueryParameters.Select = new string []{ "ext55gb1l09_msLearnCourses" };
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
//other-imports
requestParameters := &graphusers.UserItemRequestBuilderGetQueryParameters{
Select: [] string {"ext55gb1l09_msLearnCourses"},
configuration := &graphusers.UserItemRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
users, err := graphClient.Users().ByUserId("user-id").Get(context.Background(), configuration)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
User result = graphClient.users().byUserId("{user-id}").get(requestConfiguration -> {
requestConfiguration.queryParameters.select = new String []{"ext55gb1l09_msLearnCourses"};
const client = Client.init(options);
let user = await client.api('/users/4562bcc8-c436-4f95-b7c0-4f8ce89dca5e')
.select('ext55gb1l09_msLearnCourses')
.get();
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\UserItemRequestBuilderGetRequestConfiguration;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestConfiguration = new UserItemRequestBuilderGetRequestConfiguration();
$queryParameters = UserItemRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->select = ["ext55gb1l09_msLearnCourses"];
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->users()->byUserId('user-id')->get($requestConfiguration)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.users.item.user_item_request_builder import UserItemRequestBuilder
from kiota_abstractions.base_request_configuration import RequestConfiguration
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(
select = ["ext55gb1l09_msLearnCourses"],
request_configuration = RequestConfiguration(
query_parameters = query_params,
result = await graph_client.users.by_user_id('user-id').get(request_configuration = request_configuration)
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(ext55gb1l09_msLearnCourses)/$entity",
"ext55gb1l09_msLearnCourses": {
"@odata.type": "#microsoft.graph.ComplexExtensionValue",
"courseType": "Developer",
"courseName": "Introduction to Microsoft Graph",
"courseId": 1
示例 5:获取用户的自定义安全属性分配
以下示例演示如何获取用户的自定义安全属性分配。
属性 #1
属性集:Engineering
属性:Project
属性数据类型:字符串集合
属性值:["Baker","Cascade"]
属性 #2
属性集:Engineering
属性:CostCenter
属性数据类型:整数集合
属性值:[1001]
属性 #3
属性集:Engineering
属性:Certification
属性数据类型:布尔
属性值:true
属性 #4
属性集:Marketing
属性:EmployeeId
属性数据类型:字符串
属性值:"QN26904"
若要获取自定义安全属性分配,必须为调用主体分配“属性分配读取者”或“属性分配管理员”角色,并且必须被授予 CustomSecAttributeAssignment.Read.All 或 CustomSecAttributeAssignment.ReadWrite.All 权限。
有关自定义安全属性分配的更多示例,请参阅示例:使用Microsoft 图形 API分配、更新、列出或删除自定义安全属性分配。
// Code snippets are only available for the latest version. Current version is 5.x
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Users["{user-id}"].GetAsync((requestConfiguration) =>
requestConfiguration.QueryParameters.Select = new string []{ "customSecurityAttributes" };
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
//other-imports
requestParameters := &graphusers.UserItemRequestBuilderGetQueryParameters{
Select: [] string {"customSecurityAttributes"},
configuration := &graphusers.UserItemRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
users, err := graphClient.Users().ByUserId("user-id").Get(context.Background(), configuration)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
User result = graphClient.users().byUserId("{user-id}").get(requestConfiguration -> {
requestConfiguration.queryParameters.select = new String []{"customSecurityAttributes"};
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Users\Item\UserItemRequestBuilderGetRequestConfiguration;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestConfiguration = new UserItemRequestBuilderGetRequestConfiguration();
$queryParameters = UserItemRequestBuilderGetRequestConfiguration::createQueryParameters();
$queryParameters->select = ["customSecurityAttributes"];
$requestConfiguration->queryParameters = $queryParameters;
$result = $graphServiceClient->users()->byUserId('user-id')->get($requestConfiguration)->wait();
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.users.item.user_item_request_builder import UserItemRequestBuilder
from kiota_abstractions.base_request_configuration import RequestConfiguration
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
query_params = UserItemRequestBuilder.UserItemRequestBuilderGetQueryParameters(
select = ["customSecurityAttributes"],
request_configuration = RequestConfiguration(
query_parameters = query_params,
result = await graph_client.users.by_user_id('user-id').get(request_configuration = request_configuration)
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(customSecurityAttributes)/$entity",
"customSecurityAttributes": {
"Marketing": {
"@odata.type": "#microsoft.graph.customSecurityAttributeValue",
"EmployeeId": "QN26904"
"Engineering": {
"@odata.type": "#microsoft.graph.customSecurityAttributeValue",
"Project@odata.type": "#Collection(String)",
"Project": [
"Baker",
"Cascade"
"CostCenter@odata.type": "#Collection(Int32)",
"CostCenter": [
"Certification": true
如果没有向用户分配自定义安全属性,或者调用主体没有访问权限,则响应如下:
HTTP/1.1 200 OK
Content-type: application/json
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(customSecurityAttributes)/$entity",
"customSecurityAttributes": null